ASTContext.h revision 369a3bd9979cf529eed529aa037de713c213e47d
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/Basic/OperatorKinds.h"
20#include "clang/AST/Attr.h"
21#include "clang/AST/Decl.h"
22#include "clang/AST/NestedNameSpecifier.h"
23#include "clang/AST/PrettyPrinter.h"
24#include "clang/AST/TemplateName.h"
25#include "clang/AST/Type.h"
26#include "clang/AST/CanonicalType.h"
27#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/FoldingSet.h"
29#include "llvm/ADT/OwningPtr.h"
30#include "llvm/Support/Allocator.h"
31#include <vector>
32
33namespace llvm {
34  struct fltSemantics;
35}
36
37namespace clang {
38  class FileManager;
39  class ASTRecordLayout;
40  class BlockExpr;
41  class Expr;
42  class ExternalASTSource;
43  class IdentifierTable;
44  class SelectorTable;
45  class SourceManager;
46  class TargetInfo;
47  // Decls
48  class Decl;
49  class FieldDecl;
50  class ObjCIvarDecl;
51  class ObjCIvarRefExpr;
52  class ObjCPropertyDecl;
53  class RecordDecl;
54  class TagDecl;
55  class TemplateTypeParmDecl;
56  class TranslationUnitDecl;
57  class TypeDecl;
58  class TypedefDecl;
59  class UsingDecl;
60
61  namespace Builtin { class Context; }
62
63/// ASTContext - This class holds long-lived AST nodes (such as types and
64/// decls) that can be referred to throughout the semantic analysis of a file.
65class ASTContext {
66  std::vector<Type*> Types;
67  llvm::FoldingSet<ExtQuals> ExtQualNodes;
68  llvm::FoldingSet<ComplexType> ComplexTypes;
69  llvm::FoldingSet<PointerType> PointerTypes;
70  llvm::FoldingSet<BlockPointerType> BlockPointerTypes;
71  llvm::FoldingSet<LValueReferenceType> LValueReferenceTypes;
72  llvm::FoldingSet<RValueReferenceType> RValueReferenceTypes;
73  llvm::FoldingSet<MemberPointerType> MemberPointerTypes;
74  llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes;
75  llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes;
76  std::vector<VariableArrayType*> VariableArrayTypes;
77  llvm::FoldingSet<DependentSizedArrayType> DependentSizedArrayTypes;
78  llvm::FoldingSet<DependentSizedExtVectorType> DependentSizedExtVectorTypes;
79  llvm::FoldingSet<VectorType> VectorTypes;
80  llvm::FoldingSet<FunctionNoProtoType> FunctionNoProtoTypes;
81  llvm::FoldingSet<FunctionProtoType> FunctionProtoTypes;
82  llvm::FoldingSet<DependentTypeOfExprType> DependentTypeOfExprTypes;
83  llvm::FoldingSet<DependentDecltypeType> DependentDecltypeTypes;
84  llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes;
85  llvm::FoldingSet<SubstTemplateTypeParmType> SubstTemplateTypeParmTypes;
86  llvm::FoldingSet<TemplateSpecializationType> TemplateSpecializationTypes;
87  llvm::FoldingSet<QualifiedNameType> QualifiedNameTypes;
88  llvm::FoldingSet<TypenameType> TypenameTypes;
89  llvm::FoldingSet<ObjCInterfaceType> ObjCInterfaceTypes;
90  llvm::FoldingSet<ObjCObjectPointerType> ObjCObjectPointerTypes;
91  llvm::FoldingSet<ElaboratedType> ElaboratedTypes;
92
93  llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames;
94  llvm::FoldingSet<DependentTemplateName> DependentTemplateNames;
95
96  /// \brief The set of nested name specifiers.
97  ///
98  /// This set is managed by the NestedNameSpecifier class.
99  llvm::FoldingSet<NestedNameSpecifier> NestedNameSpecifiers;
100  NestedNameSpecifier *GlobalNestedNameSpecifier;
101  friend class NestedNameSpecifier;
102
103  /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts.
104  ///  This is lazily created.  This is intentionally not serialized.
105  llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts;
106  llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*> ObjCLayouts;
107
108  /// \brief Mapping from ObjCContainers to their ObjCImplementations.
109  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*> ObjCImpls;
110
111  llvm::DenseMap<unsigned, FixedWidthIntType*> SignedFixedWidthIntTypes;
112  llvm::DenseMap<unsigned, FixedWidthIntType*> UnsignedFixedWidthIntTypes;
113
114  /// BuiltinVaListType - built-in va list type.
115  /// This is initially null and set by Sema::LazilyCreateBuiltin when
116  /// a builtin that takes a valist is encountered.
117  QualType BuiltinVaListType;
118
119  /// ObjCIdType - a pseudo built-in typedef type (set by Sema).
120  QualType ObjCIdTypedefType;
121
122  /// ObjCSelType - another pseudo built-in typedef type (set by Sema).
123  QualType ObjCSelTypedefType;
124
125  /// ObjCProtoType - another pseudo built-in typedef type (set by Sema).
126  QualType ObjCProtoType;
127  const RecordType *ProtoStructType;
128
129  /// ObjCClassType - another pseudo built-in typedef type (set by Sema).
130  QualType ObjCClassTypedefType;
131
132  QualType ObjCConstantStringType;
133  RecordDecl *CFConstantStringTypeDecl;
134
135  RecordDecl *ObjCFastEnumerationStateTypeDecl;
136
137  /// \brief The type for the C FILE type.
138  TypeDecl *FILEDecl;
139
140  /// \brief The type for the C jmp_buf type.
141  TypeDecl *jmp_bufDecl;
142
143  /// \brief The type for the C sigjmp_buf type.
144  TypeDecl *sigjmp_bufDecl;
145
146  /// \brief Type for the Block descriptor for Blocks CodeGen.
147  RecordDecl *BlockDescriptorType;
148
149  /// \brief Type for the Block descriptor for Blocks CodeGen.
150  RecordDecl *BlockDescriptorExtendedType;
151
152  /// \brief Keeps track of all declaration attributes.
153  ///
154  /// Since so few decls have attrs, we keep them in a hash map instead of
155  /// wasting space in the Decl class.
156  llvm::DenseMap<const Decl*, Attr*> DeclAttrs;
157
158  /// \brief Keeps track of the static data member templates from which
159  /// static data members of class template specializations were instantiated.
160  ///
161  /// This data structure stores the mapping from instantiations of static
162  /// data members to the static data member representations within the
163  /// class template from which they were instantiated along with the kind
164  /// of instantiation or specialization (a TemplateSpecializationKind - 1).
165  ///
166  /// Given the following example:
167  ///
168  /// \code
169  /// template<typename T>
170  /// struct X {
171  ///   static T value;
172  /// };
173  ///
174  /// template<typename T>
175  ///   T X<T>::value = T(17);
176  ///
177  /// int *x = &X<int>::value;
178  /// \endcode
179  ///
180  /// This mapping will contain an entry that maps from the VarDecl for
181  /// X<int>::value to the corresponding VarDecl for X<T>::value (within the
182  /// class template X) and will be marked TSK_ImplicitInstantiation.
183  llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>
184    InstantiatedFromStaticDataMember;
185
186  /// \brief Keeps track of the UnresolvedUsingDecls from which UsingDecls
187  /// where created during instantiation.
188  ///
189  /// For example:
190  /// \code
191  /// template<typename T>
192  /// struct A {
193  ///   void f();
194  /// };
195  ///
196  /// template<typename T>
197  /// struct B : A<T> {
198  ///   using A<T>::f;
199  /// };
200  ///
201  /// template struct B<int>;
202  /// \endcode
203  ///
204  /// This mapping will contain an entry that maps from the UsingDecl in
205  /// B<int> to the UnresolvedUsingDecl in B<T>.
206  llvm::DenseMap<UsingDecl *, NamedDecl *>
207    InstantiatedFromUnresolvedUsingDecl;
208
209  llvm::DenseMap<FieldDecl *, FieldDecl *> InstantiatedFromUnnamedFieldDecl;
210
211  TranslationUnitDecl *TUDecl;
212
213  /// SourceMgr - The associated SourceManager object.
214  SourceManager &SourceMgr;
215
216  /// LangOpts - The language options used to create the AST associated with
217  ///  this ASTContext object.
218  LangOptions LangOpts;
219
220  /// \brief Whether we have already loaded comment source ranges from an
221  /// external source.
222  bool LoadedExternalComments;
223
224  /// MallocAlloc/BumpAlloc - The allocator objects used to create AST objects.
225  bool FreeMemory;
226  llvm::MallocAllocator MallocAlloc;
227  llvm::BumpPtrAllocator BumpAlloc;
228
229  /// \brief Mapping from declarations to their comments, once we have
230  /// already looked up the comment associated with a given declaration.
231  llvm::DenseMap<const Decl *, std::string> DeclComments;
232
233public:
234  const TargetInfo &Target;
235  IdentifierTable &Idents;
236  SelectorTable &Selectors;
237  Builtin::Context &BuiltinInfo;
238  DeclarationNameTable DeclarationNames;
239  llvm::OwningPtr<ExternalASTSource> ExternalSource;
240  clang::PrintingPolicy PrintingPolicy;
241
242  // Typedefs which may be provided defining the structure of Objective-C
243  // pseudo-builtins
244  QualType ObjCIdRedefinitionType;
245  QualType ObjCClassRedefinitionType;
246  QualType ObjCSelRedefinitionType;
247
248  /// \brief Source ranges for all of the comments in the source file,
249  /// sorted in order of appearance in the translation unit.
250  std::vector<SourceRange> Comments;
251
252  SourceManager& getSourceManager() { return SourceMgr; }
253  const SourceManager& getSourceManager() const { return SourceMgr; }
254  void *Allocate(unsigned Size, unsigned Align = 8) {
255    return FreeMemory ? MallocAlloc.Allocate(Size, Align) :
256                        BumpAlloc.Allocate(Size, Align);
257  }
258  void Deallocate(void *Ptr) {
259    if (FreeMemory)
260      MallocAlloc.Deallocate(Ptr);
261  }
262  const LangOptions& getLangOptions() const { return LangOpts; }
263
264  FullSourceLoc getFullLoc(SourceLocation Loc) const {
265    return FullSourceLoc(Loc,SourceMgr);
266  }
267
268  /// \brief Retrieve the attributes for the given declaration.
269  Attr*& getDeclAttrs(const Decl *D) { return DeclAttrs[D]; }
270
271  /// \brief Erase the attributes corresponding to the given declaration.
272  void eraseDeclAttrs(const Decl *D) { DeclAttrs.erase(D); }
273
274  /// \brief If this variable is an instantiated static data member of a
275  /// class template specialization, returns the templated static data member
276  /// from which it was instantiated.
277  MemberSpecializationInfo *getInstantiatedFromStaticDataMember(
278                                                           const VarDecl *Var);
279
280  /// \brief Note that the static data member \p Inst is an instantiation of
281  /// the static data member template \p Tmpl of a class template.
282  void setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
283                                           TemplateSpecializationKind TSK);
284
285  /// \brief If this using decl is instantiated from an unresolved using decl,
286  /// return it.
287  NamedDecl *getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD);
288
289  /// \brief Note that the using decl \p Inst is an instantiation of
290  /// the unresolved using decl \p Tmpl of a class template.
291  void setInstantiatedFromUnresolvedUsingDecl(UsingDecl *Inst, NamedDecl *Tmpl);
292
293
294  FieldDecl *getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field);
295
296  void setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, FieldDecl *Tmpl);
297
298  TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; }
299
300
301  const char *getCommentForDecl(const Decl *D);
302
303  // Builtin Types.
304  CanQualType VoidTy;
305  CanQualType BoolTy;
306  CanQualType CharTy;
307  CanQualType WCharTy;  // [C++ 3.9.1p5], integer type in C99.
308  CanQualType Char16Ty; // [C++0x 3.9.1p5], integer type in C99.
309  CanQualType Char32Ty; // [C++0x 3.9.1p5], integer type in C99.
310  CanQualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy, Int128Ty;
311  CanQualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
312  CanQualType UnsignedLongLongTy, UnsignedInt128Ty;
313  CanQualType FloatTy, DoubleTy, LongDoubleTy;
314  CanQualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
315  CanQualType VoidPtrTy, NullPtrTy;
316  CanQualType OverloadTy;
317  CanQualType DependentTy;
318  CanQualType UndeducedAutoTy;
319  CanQualType ObjCBuiltinIdTy, ObjCBuiltinClassTy, ObjCBuiltinSelTy;
320
321  ASTContext(const LangOptions& LOpts, SourceManager &SM, const TargetInfo &t,
322             IdentifierTable &idents, SelectorTable &sels,
323             Builtin::Context &builtins,
324             bool FreeMemory = true, unsigned size_reserve=0);
325
326  ~ASTContext();
327
328  /// \brief Attach an external AST source to the AST context.
329  ///
330  /// The external AST source provides the ability to load parts of
331  /// the abstract syntax tree as needed from some external storage,
332  /// e.g., a precompiled header.
333  void setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source);
334
335  /// \brief Retrieve a pointer to the external AST source associated
336  /// with this AST context, if any.
337  ExternalASTSource *getExternalSource() const { return ExternalSource.get(); }
338
339  void PrintStats() const;
340  const std::vector<Type*>& getTypes() const { return Types; }
341
342  //===--------------------------------------------------------------------===//
343  //                           Type Constructors
344  //===--------------------------------------------------------------------===//
345
346private:
347  /// getExtQualType - Return a type with extended qualifiers.
348  QualType getExtQualType(const Type *Base, Qualifiers Quals);
349
350public:
351  /// getAddSpaceQualType - Return the uniqued reference to the type for an
352  /// address space qualified type with the specified type and address space.
353  /// The resulting type has a union of the qualifiers from T and the address
354  /// space. If T already has an address space specifier, it is silently
355  /// replaced.
356  QualType getAddrSpaceQualType(QualType T, unsigned AddressSpace);
357
358  /// getObjCGCQualType - Returns the uniqued reference to the type for an
359  /// objc gc qualified type. The retulting type has a union of the qualifiers
360  /// from T and the gc attribute.
361  QualType getObjCGCQualType(QualType T, Qualifiers::GC gcAttr);
362
363  /// getRestrictType - Returns the uniqued reference to the type for a
364  /// 'restrict' qualified type.  The resulting type has a union of the
365  /// qualifiers from T and 'restrict'.
366  QualType getRestrictType(QualType T) {
367    return T.withFastQualifiers(Qualifiers::Restrict);
368  }
369
370  /// getVolatileType - Returns the uniqued reference to the type for a
371  /// 'volatile' qualified type.  The resulting type has a union of the
372  /// qualifiers from T and 'volatile'.
373  QualType getVolatileType(QualType T);
374
375  /// getConstType - Returns the uniqued reference to the type for a
376  /// 'const' qualified type.  The resulting type has a union of the
377  /// qualifiers from T and 'const'.
378  ///
379  /// It can be reasonably expected that this will always be
380  /// equivalent to calling T.withConst().
381  QualType getConstType(QualType T) { return T.withConst(); }
382
383  /// getNoReturnType - Add the noreturn attribute to the given type which must
384  /// be a FunctionType or a pointer to an allowable type or a BlockPointer.
385  QualType getNoReturnType(QualType T);
386
387  /// getComplexType - Return the uniqued reference to the type for a complex
388  /// number with the specified element type.
389  QualType getComplexType(QualType T);
390  CanQualType getComplexType(CanQualType T) {
391    return CanQualType::CreateUnsafe(getComplexType((QualType) T));
392  }
393
394  /// getPointerType - Return the uniqued reference to the type for a pointer to
395  /// the specified type.
396  QualType getPointerType(QualType T);
397  CanQualType getPointerType(CanQualType T) {
398    return CanQualType::CreateUnsafe(getPointerType((QualType) T));
399  }
400
401  /// getBlockPointerType - Return the uniqued reference to the type for a block
402  /// of the specified type.
403  QualType getBlockPointerType(QualType T);
404
405  /// This gets the struct used to keep track of the descriptor for pointer to
406  /// blocks.
407  QualType getBlockDescriptorType();
408
409  // Set the type for a Block descriptor type.
410  void setBlockDescriptorType(QualType T);
411  /// Get the BlockDescriptorType type, or NULL if it hasn't yet been built.
412  QualType getRawBlockdescriptorType() {
413    if (BlockDescriptorType)
414      return getTagDeclType(BlockDescriptorType);
415    return QualType();
416  }
417
418  /// This gets the struct used to keep track of the extended descriptor for
419  /// pointer to blocks.
420  QualType getBlockDescriptorExtendedType();
421
422  // Set the type for a Block descriptor extended type.
423  void setBlockDescriptorExtendedType(QualType T);
424  /// Get the BlockDescriptorExtendedType type, or NULL if it hasn't yet been
425  /// built.
426  QualType getRawBlockdescriptorExtendedType() {
427    if (BlockDescriptorExtendedType)
428      return getTagDeclType(BlockDescriptorExtendedType);
429    return QualType();
430  }
431
432  /// This gets the struct used to keep track of pointer to blocks, complete
433  /// with captured variables.
434  QualType getBlockParmType(bool BlockHasCopyDispose,
435                            llvm::SmallVector<const Expr *, 8> &BDRDs);
436
437  /// This builds the struct used for __block variables.
438  QualType BuildByRefType(const char *DeclName, QualType Ty);
439
440  /// Returns true iff we need copy/dispose helpers for the given type.
441  bool BlockRequiresCopying(QualType Ty);
442
443  /// getLValueReferenceType - Return the uniqued reference to the type for an
444  /// lvalue reference to the specified type.
445  QualType getLValueReferenceType(QualType T, bool SpelledAsLValue = true);
446
447  /// getRValueReferenceType - Return the uniqued reference to the type for an
448  /// rvalue reference to the specified type.
449  QualType getRValueReferenceType(QualType T);
450
451  /// getMemberPointerType - Return the uniqued reference to the type for a
452  /// member pointer to the specified type in the specified class. The class
453  /// is a Type because it could be a dependent name.
454  QualType getMemberPointerType(QualType T, const Type *Cls);
455
456  /// getVariableArrayType - Returns a non-unique reference to the type for a
457  /// variable array of the specified element type.
458  QualType getVariableArrayType(QualType EltTy, Expr *NumElts,
459                                ArrayType::ArraySizeModifier ASM,
460                                unsigned EltTypeQuals,
461                                SourceRange Brackets);
462
463  /// getDependentSizedArrayType - Returns a non-unique reference to
464  /// the type for a dependently-sized array of the specified element
465  /// type. FIXME: We will need these to be uniqued, or at least
466  /// comparable, at some point.
467  QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
468                                      ArrayType::ArraySizeModifier ASM,
469                                      unsigned EltTypeQuals,
470                                      SourceRange Brackets);
471
472  /// getIncompleteArrayType - Returns a unique reference to the type for a
473  /// incomplete array of the specified element type.
474  QualType getIncompleteArrayType(QualType EltTy,
475                                  ArrayType::ArraySizeModifier ASM,
476                                  unsigned EltTypeQuals);
477
478  /// getConstantArrayType - Return the unique reference to the type for a
479  /// constant array of the specified element type.
480  QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize,
481                                ArrayType::ArraySizeModifier ASM,
482                                unsigned EltTypeQuals);
483
484  /// getVectorType - Return the unique reference to a vector type of
485  /// the specified element type and size. VectorType must be a built-in type.
486  QualType getVectorType(QualType VectorType, unsigned NumElts);
487
488  /// getExtVectorType - Return the unique reference to an extended vector type
489  /// of the specified element type and size.  VectorType must be a built-in
490  /// type.
491  QualType getExtVectorType(QualType VectorType, unsigned NumElts);
492
493  /// getDependentSizedExtVectorType - Returns a non-unique reference to
494  /// the type for a dependently-sized vector of the specified element
495  /// type. FIXME: We will need these to be uniqued, or at least
496  /// comparable, at some point.
497  QualType getDependentSizedExtVectorType(QualType VectorType,
498                                          Expr *SizeExpr,
499                                          SourceLocation AttrLoc);
500
501  /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
502  ///
503  QualType getFunctionNoProtoType(QualType ResultTy, bool NoReturn = false);
504
505  /// getFunctionType - Return a normal function type with a typed argument
506  /// list.  isVariadic indicates whether the argument list includes '...'.
507  QualType getFunctionType(QualType ResultTy, const QualType *ArgArray,
508                           unsigned NumArgs, bool isVariadic,
509                           unsigned TypeQuals, bool hasExceptionSpec = false,
510                           bool hasAnyExceptionSpec = false,
511                           unsigned NumExs = 0, const QualType *ExArray = 0,
512                           bool NoReturn = false);
513
514  /// getTypeDeclType - Return the unique reference to the type for
515  /// the specified type declaration.
516  QualType getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl=0);
517
518  /// getTypedefType - Return the unique reference to the type for the
519  /// specified typename decl.
520  QualType getTypedefType(TypedefDecl *Decl);
521
522  QualType getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced,
523                                        QualType Replacement);
524
525  QualType getTemplateTypeParmType(unsigned Depth, unsigned Index,
526                                   bool ParameterPack,
527                                   IdentifierInfo *Name = 0);
528
529  QualType getTemplateSpecializationType(TemplateName T,
530                                         const TemplateArgument *Args,
531                                         unsigned NumArgs,
532                                         QualType Canon = QualType());
533
534  QualType getTemplateSpecializationType(TemplateName T,
535                                         const TemplateArgumentListInfo &Args,
536                                         QualType Canon = QualType());
537
538  QualType getQualifiedNameType(NestedNameSpecifier *NNS,
539                                QualType NamedType);
540  QualType getTypenameType(NestedNameSpecifier *NNS,
541                           const IdentifierInfo *Name,
542                           QualType Canon = QualType());
543  QualType getTypenameType(NestedNameSpecifier *NNS,
544                           const TemplateSpecializationType *TemplateId,
545                           QualType Canon = QualType());
546  QualType getElaboratedType(QualType UnderlyingType,
547                             ElaboratedType::TagKind Tag);
548
549  QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
550                                ObjCProtocolDecl **Protocols = 0,
551                                unsigned NumProtocols = 0);
552
553  /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for the
554  /// given interface decl and the conforming protocol list.
555  QualType getObjCObjectPointerType(QualType OIT,
556                                    ObjCProtocolDecl **ProtocolList = 0,
557                                    unsigned NumProtocols = 0);
558
559  /// getTypeOfType - GCC extension.
560  QualType getTypeOfExprType(Expr *e);
561  QualType getTypeOfType(QualType t);
562
563  /// getDecltypeType - C++0x decltype.
564  QualType getDecltypeType(Expr *e);
565
566  /// getTagDeclType - Return the unique reference to the type for the
567  /// specified TagDecl (struct/union/class/enum) decl.
568  QualType getTagDeclType(const TagDecl *Decl);
569
570  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
571  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
572  QualType getSizeType() const;
573
574  /// getWCharType - In C++, this returns the unique wchar_t type.  In C99, this
575  /// returns a type compatible with the type defined in <stddef.h> as defined
576  /// by the target.
577  QualType getWCharType() const { return WCharTy; }
578
579  /// getSignedWCharType - Return the type of "signed wchar_t".
580  /// Used when in C++, as a GCC extension.
581  QualType getSignedWCharType() const;
582
583  /// getUnsignedWCharType - Return the type of "unsigned wchar_t".
584  /// Used when in C++, as a GCC extension.
585  QualType getUnsignedWCharType() const;
586
587  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
588  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
589  QualType getPointerDiffType() const;
590
591  // getCFConstantStringType - Return the C structure type used to represent
592  // constant CFStrings.
593  QualType getCFConstantStringType();
594
595  /// Get the structure type used to representation CFStrings, or NULL
596  /// if it hasn't yet been built.
597  QualType getRawCFConstantStringType() {
598    if (CFConstantStringTypeDecl)
599      return getTagDeclType(CFConstantStringTypeDecl);
600    return QualType();
601  }
602  void setCFConstantStringType(QualType T);
603
604  // This setter/getter represents the ObjC type for an NSConstantString.
605  void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl);
606  QualType getObjCConstantStringInterface() const {
607    return ObjCConstantStringType;
608  }
609
610  //// This gets the struct used to keep track of fast enumerations.
611  QualType getObjCFastEnumerationStateType();
612
613  /// Get the ObjCFastEnumerationState type, or NULL if it hasn't yet
614  /// been built.
615  QualType getRawObjCFastEnumerationStateType() {
616    if (ObjCFastEnumerationStateTypeDecl)
617      return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
618    return QualType();
619  }
620
621  void setObjCFastEnumerationStateType(QualType T);
622
623  /// \brief Set the type for the C FILE type.
624  void setFILEDecl(TypeDecl *FILEDecl) { this->FILEDecl = FILEDecl; }
625
626  /// \brief Retrieve the C FILE type.
627  QualType getFILEType() {
628    if (FILEDecl)
629      return getTypeDeclType(FILEDecl);
630    return QualType();
631  }
632
633  /// \brief Set the type for the C jmp_buf type.
634  void setjmp_bufDecl(TypeDecl *jmp_bufDecl) {
635    this->jmp_bufDecl = jmp_bufDecl;
636  }
637
638  /// \brief Retrieve the C jmp_buf type.
639  QualType getjmp_bufType() {
640    if (jmp_bufDecl)
641      return getTypeDeclType(jmp_bufDecl);
642    return QualType();
643  }
644
645  /// \brief Set the type for the C sigjmp_buf type.
646  void setsigjmp_bufDecl(TypeDecl *sigjmp_bufDecl) {
647    this->sigjmp_bufDecl = sigjmp_bufDecl;
648  }
649
650  /// \brief Retrieve the C sigjmp_buf type.
651  QualType getsigjmp_bufType() {
652    if (sigjmp_bufDecl)
653      return getTypeDeclType(sigjmp_bufDecl);
654    return QualType();
655  }
656
657  /// getObjCEncodingForType - Emit the ObjC type encoding for the
658  /// given type into \arg S. If \arg NameFields is specified then
659  /// record field names are also encoded.
660  void getObjCEncodingForType(QualType t, std::string &S,
661                              const FieldDecl *Field=0);
662
663  void getLegacyIntegralTypeEncoding(QualType &t) const;
664
665  // Put the string version of type qualifiers into S.
666  void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
667                                       std::string &S) const;
668
669  /// getObjCEncodingForMethodDecl - Return the encoded type for this method
670  /// declaration.
671  void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S);
672
673  /// getObjCEncodingForBlockDecl - Return the encoded type for this block
674  /// declaration.
675  void getObjCEncodingForBlock(const BlockExpr *Expr, std::string& S);
676
677  /// getObjCEncodingForPropertyDecl - Return the encoded type for
678  /// this method declaration. If non-NULL, Container must be either
679  /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should
680  /// only be NULL when getting encodings for protocol properties.
681  void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
682                                      const Decl *Container,
683                                      std::string &S);
684
685  bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
686                                      ObjCProtocolDecl *rProto);
687
688  /// getObjCEncodingTypeSize returns size of type for objective-c encoding
689  /// purpose.
690  int getObjCEncodingTypeSize(QualType t);
691
692  /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by
693  /// Sema.  id is always a (typedef for a) pointer type, a pointer to a struct.
694  QualType getObjCIdType() const { return ObjCIdTypedefType; }
695  void setObjCIdType(QualType T);
696
697  void setObjCSelType(QualType T);
698  QualType getObjCSelType() const { return ObjCSelTypedefType; }
699
700  void setObjCProtoType(QualType QT);
701  QualType getObjCProtoType() const { return ObjCProtoType; }
702
703  /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by
704  /// Sema.  'Class' is always a (typedef for a) pointer type, a pointer to a
705  /// struct.
706  QualType getObjCClassType() const { return ObjCClassTypedefType; }
707  void setObjCClassType(QualType T);
708
709  void setBuiltinVaListType(QualType T);
710  QualType getBuiltinVaListType() const { return BuiltinVaListType; }
711
712  QualType getFixedWidthIntType(unsigned Width, bool Signed);
713
714  /// getCVRQualifiedType - Returns a type with additional const,
715  /// volatile, or restrict qualifiers.
716  QualType getCVRQualifiedType(QualType T, unsigned CVR) {
717    return getQualifiedType(T, Qualifiers::fromCVRMask(CVR));
718  }
719
720  /// getQualifiedType - Returns a type with additional qualifiers.
721  QualType getQualifiedType(QualType T, Qualifiers Qs) {
722    if (!Qs.hasNonFastQualifiers())
723      return T.withFastQualifiers(Qs.getFastQualifiers());
724    QualifierCollector Qc(Qs);
725    const Type *Ptr = Qc.strip(T);
726    return getExtQualType(Ptr, Qc);
727  }
728
729  /// getQualifiedType - Returns a type with additional qualifiers.
730  QualType getQualifiedType(const Type *T, Qualifiers Qs) {
731    if (!Qs.hasNonFastQualifiers())
732      return QualType(T, Qs.getFastQualifiers());
733    return getExtQualType(T, Qs);
734  }
735
736  DeclarationName getNameForTemplate(TemplateName Name);
737
738  TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS,
739                                        bool TemplateKeyword,
740                                        TemplateDecl *Template);
741  TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS,
742                                        bool TemplateKeyword,
743                                        OverloadedFunctionDecl *Template);
744
745  TemplateName getDependentTemplateName(NestedNameSpecifier *NNS,
746                                        const IdentifierInfo *Name);
747  TemplateName getDependentTemplateName(NestedNameSpecifier *NNS,
748                                        OverloadedOperatorKind Operator);
749
750  enum GetBuiltinTypeError {
751    GE_None,              //< No error
752    GE_Missing_stdio,     //< Missing a type from <stdio.h>
753    GE_Missing_setjmp     //< Missing a type from <setjmp.h>
754  };
755
756  /// GetBuiltinType - Return the type for the specified builtin.
757  QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error);
758
759private:
760  CanQualType getFromTargetType(unsigned Type) const;
761
762  //===--------------------------------------------------------------------===//
763  //                         Type Predicates.
764  //===--------------------------------------------------------------------===//
765
766public:
767  /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
768  /// garbage collection attribute.
769  ///
770  Qualifiers::GC getObjCGCAttrKind(const QualType &Ty) const;
771
772  /// isObjCNSObjectType - Return true if this is an NSObject object with
773  /// its NSObject attribute set.
774  bool isObjCNSObjectType(QualType Ty) const;
775
776  //===--------------------------------------------------------------------===//
777  //                         Type Sizing and Analysis
778  //===--------------------------------------------------------------------===//
779
780  /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
781  /// scalar floating point type.
782  const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const;
783
784  /// getTypeInfo - Get the size and alignment of the specified complete type in
785  /// bits.
786  std::pair<uint64_t, unsigned> getTypeInfo(const Type *T);
787  std::pair<uint64_t, unsigned> getTypeInfo(QualType T) {
788    return getTypeInfo(T.getTypePtr());
789  }
790
791  /// getTypeSize - Return the size of the specified type, in bits.  This method
792  /// does not work on incomplete types.
793  uint64_t getTypeSize(QualType T) {
794    return getTypeInfo(T).first;
795  }
796  uint64_t getTypeSize(const Type *T) {
797    return getTypeInfo(T).first;
798  }
799
800  /// getByteWidth - Return the size of a byte, in bits
801  uint64_t getByteSize() {
802    return getTypeSize(CharTy);
803  }
804
805  /// getTypeSizeInBytes - Return the size of the specified type, in bytes.
806  /// This method does not work on incomplete types.
807  uint64_t getTypeSizeInBytes(QualType T) {
808    return getTypeSize(T) / getByteSize();
809  }
810  uint64_t getTypeSizeInBytes(const Type *T) {
811    return getTypeSize(T) / getByteSize();
812  }
813
814  /// getTypeAlign - Return the ABI-specified alignment of a type, in bits.
815  /// This method does not work on incomplete types.
816  unsigned getTypeAlign(QualType T) {
817    return getTypeInfo(T).second;
818  }
819  unsigned getTypeAlign(const Type *T) {
820    return getTypeInfo(T).second;
821  }
822
823  /// getPreferredTypeAlign - Return the "preferred" alignment of the specified
824  /// type for the current target in bits.  This can be different than the ABI
825  /// alignment in cases where it is beneficial for performance to overalign
826  /// a data type.
827  unsigned getPreferredTypeAlign(const Type *T);
828
829  unsigned getDeclAlignInBytes(const Decl *D, bool RefAsPointee = false);
830
831  /// getASTRecordLayout - Get or compute information about the layout of the
832  /// specified record (struct/union/class), which indicates its size and field
833  /// position information.
834  const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D);
835
836  /// getASTObjCInterfaceLayout - Get or compute information about the
837  /// layout of the specified Objective-C interface.
838  const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D);
839
840  /// getASTObjCImplementationLayout - Get or compute information about
841  /// the layout of the specified Objective-C implementation. This may
842  /// differ from the interface if synthesized ivars are present.
843  const ASTRecordLayout &
844  getASTObjCImplementationLayout(const ObjCImplementationDecl *D);
845
846  void CollectObjCIvars(const ObjCInterfaceDecl *OI,
847                        llvm::SmallVectorImpl<FieldDecl*> &Fields);
848
849  void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
850                               llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
851                               bool CollectSynthesized = true);
852  void CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
853                               llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars);
854  void CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
855                               llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars);
856  unsigned CountSynthesizedIvars(const ObjCInterfaceDecl *OI);
857  unsigned CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD);
858  void CollectInheritedProtocols(const Decl *CDecl,
859                          llvm::SmallVectorImpl<ObjCProtocolDecl*> &Protocols);
860
861  //===--------------------------------------------------------------------===//
862  //                            Type Operators
863  //===--------------------------------------------------------------------===//
864
865  /// getCanonicalType - Return the canonical (structural) type corresponding to
866  /// the specified potentially non-canonical type.  The non-canonical version
867  /// of a type may have many "decorated" versions of types.  Decorators can
868  /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
869  /// to be free of any of these, allowing two canonical types to be compared
870  /// for exact equality with a simple pointer comparison.
871  CanQualType getCanonicalType(QualType T);
872  const Type *getCanonicalType(const Type *T) {
873    return T->getCanonicalTypeInternal().getTypePtr();
874  }
875
876  /// getCanonicalParamType - Return the canonical parameter type
877  /// corresponding to the specific potentially non-canonical one.
878  /// Qualifiers are stripped off, functions are turned into function
879  /// pointers, and arrays decay one level into pointers.
880  CanQualType getCanonicalParamType(QualType T);
881
882  /// \brief Determine whether the given types are equivalent.
883  bool hasSameType(QualType T1, QualType T2) {
884    return getCanonicalType(T1) == getCanonicalType(T2);
885  }
886
887  /// \brief Determine whether the given types are equivalent after
888  /// cvr-qualifiers have been removed.
889  bool hasSameUnqualifiedType(QualType T1, QualType T2) {
890    CanQualType CT1 = getCanonicalType(T1);
891    CanQualType CT2 = getCanonicalType(T2);
892    return CT1.getUnqualifiedType() == CT2.getUnqualifiedType();
893  }
894
895  /// \brief Retrieves the "canonical" declaration of
896
897  /// \brief Retrieves the "canonical" nested name specifier for a
898  /// given nested name specifier.
899  ///
900  /// The canonical nested name specifier is a nested name specifier
901  /// that uniquely identifies a type or namespace within the type
902  /// system. For example, given:
903  ///
904  /// \code
905  /// namespace N {
906  ///   struct S {
907  ///     template<typename T> struct X { typename T* type; };
908  ///   };
909  /// }
910  ///
911  /// template<typename T> struct Y {
912  ///   typename N::S::X<T>::type member;
913  /// };
914  /// \endcode
915  ///
916  /// Here, the nested-name-specifier for N::S::X<T>:: will be
917  /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined
918  /// by declarations in the type system and the canonical type for
919  /// the template type parameter 'T' is template-param-0-0.
920  NestedNameSpecifier *
921  getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS);
922
923  /// \brief Retrieves the "canonical" template name that refers to a
924  /// given template.
925  ///
926  /// The canonical template name is the simplest expression that can
927  /// be used to refer to a given template. For most templates, this
928  /// expression is just the template declaration itself. For example,
929  /// the template std::vector can be referred to via a variety of
930  /// names---std::vector, ::std::vector, vector (if vector is in
931  /// scope), etc.---but all of these names map down to the same
932  /// TemplateDecl, which is used to form the canonical template name.
933  ///
934  /// Dependent template names are more interesting. Here, the
935  /// template name could be something like T::template apply or
936  /// std::allocator<T>::template rebind, where the nested name
937  /// specifier itself is dependent. In this case, the canonical
938  /// template name uses the shortest form of the dependent
939  /// nested-name-specifier, which itself contains all canonical
940  /// types, values, and templates.
941  TemplateName getCanonicalTemplateName(TemplateName Name);
942
943  /// \brief Determine whether the given template names refer to the same
944  /// template.
945  bool hasSameTemplateName(TemplateName X, TemplateName Y);
946
947  /// \brief Retrieve the "canonical" template argument.
948  ///
949  /// The canonical template argument is the simplest template argument
950  /// (which may be a type, value, expression, or declaration) that
951  /// expresses the value of the argument.
952  TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg);
953
954  /// Type Query functions.  If the type is an instance of the specified class,
955  /// return the Type pointer for the underlying maximally pretty type.  This
956  /// is a member of ASTContext because this may need to do some amount of
957  /// canonicalization, e.g. to move type qualifiers into the element type.
958  const ArrayType *getAsArrayType(QualType T);
959  const ConstantArrayType *getAsConstantArrayType(QualType T) {
960    return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T));
961  }
962  const VariableArrayType *getAsVariableArrayType(QualType T) {
963    return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T));
964  }
965  const IncompleteArrayType *getAsIncompleteArrayType(QualType T) {
966    return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T));
967  }
968
969  /// getBaseElementType - Returns the innermost element type of an array type.
970  /// For example, will return "int" for int[m][n]
971  QualType getBaseElementType(const ArrayType *VAT);
972
973  /// getBaseElementType - Returns the innermost element type of a type
974  /// (which needn't actually be an array type).
975  QualType getBaseElementType(QualType QT);
976
977  /// getConstantArrayElementCount - Returns number of constant array elements.
978  uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const;
979
980  /// getArrayDecayedType - Return the properly qualified result of decaying the
981  /// specified array type to a pointer.  This operation is non-trivial when
982  /// handling typedefs etc.  The canonical type of "T" must be an array type,
983  /// this returns a pointer to a properly qualified element of the array.
984  ///
985  /// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
986  QualType getArrayDecayedType(QualType T);
987
988  /// getPromotedIntegerType - Returns the type that Promotable will
989  /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
990  /// integer type.
991  QualType getPromotedIntegerType(QualType PromotableType);
992
993  /// \brief Whether this is a promotable bitfield reference according
994  /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
995  ///
996  /// \returns the type this bit-field will promote to, or NULL if no
997  /// promotion occurs.
998  QualType isPromotableBitField(Expr *E);
999
1000  /// getIntegerTypeOrder - Returns the highest ranked integer type:
1001  /// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
1002  /// LHS < RHS, return -1.
1003  int getIntegerTypeOrder(QualType LHS, QualType RHS);
1004
1005  /// getFloatingTypeOrder - Compare the rank of the two specified floating
1006  /// point types, ignoring the domain of the type (i.e. 'double' ==
1007  /// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
1008  /// LHS < RHS, return -1.
1009  int getFloatingTypeOrder(QualType LHS, QualType RHS);
1010
1011  /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1012  /// point or a complex type (based on typeDomain/typeSize).
1013  /// 'typeDomain' is a real floating point or complex type.
1014  /// 'typeSize' is a real floating point or complex type.
1015  QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
1016                                             QualType typeDomain) const;
1017
1018private:
1019  // Helper for integer ordering
1020  unsigned getIntegerRank(Type* T);
1021
1022public:
1023
1024  //===--------------------------------------------------------------------===//
1025  //                    Type Compatibility Predicates
1026  //===--------------------------------------------------------------------===//
1027
1028  /// Compatibility predicates used to check assignment expressions.
1029  bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1
1030
1031  bool isObjCIdType(QualType T) const {
1032    return T == ObjCIdTypedefType;
1033  }
1034  bool isObjCClassType(QualType T) const {
1035    return T == ObjCClassTypedefType;
1036  }
1037  bool isObjCSelType(QualType T) const {
1038    return T == ObjCSelTypedefType;
1039  }
1040  bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS);
1041  bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS,
1042                                         bool ForCompare);
1043
1044  // Check the safety of assignment from LHS to RHS
1045  bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
1046                               const ObjCObjectPointerType *RHSOPT);
1047  bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1048                               const ObjCInterfaceType *RHS);
1049  bool areComparableObjCPointerTypes(QualType LHS, QualType RHS);
1050  QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT,
1051                                   const ObjCObjectPointerType *RHSOPT);
1052
1053  // Functions for calculating composite types
1054  QualType mergeTypes(QualType, QualType);
1055  QualType mergeFunctionTypes(QualType, QualType);
1056
1057  /// UsualArithmeticConversionsType - handles the various conversions
1058  /// that are common to binary operators (C99 6.3.1.8, C++ [expr]p9)
1059  /// and returns the result type of that conversion.
1060  QualType UsualArithmeticConversionsType(QualType lhs, QualType rhs);
1061
1062  //===--------------------------------------------------------------------===//
1063  //                    Integer Predicates
1064  //===--------------------------------------------------------------------===//
1065
1066  // The width of an integer, as defined in C99 6.2.6.2. This is the number
1067  // of bits in an integer type excluding any padding bits.
1068  unsigned getIntWidth(QualType T);
1069
1070  // Per C99 6.2.5p6, for every signed integer type, there is a corresponding
1071  // unsigned integer type.  This method takes a signed type, and returns the
1072  // corresponding unsigned integer type.
1073  QualType getCorrespondingUnsignedType(QualType T);
1074
1075  //===--------------------------------------------------------------------===//
1076  //                    Type Iterators.
1077  //===--------------------------------------------------------------------===//
1078
1079  typedef std::vector<Type*>::iterator       type_iterator;
1080  typedef std::vector<Type*>::const_iterator const_type_iterator;
1081
1082  type_iterator types_begin() { return Types.begin(); }
1083  type_iterator types_end() { return Types.end(); }
1084  const_type_iterator types_begin() const { return Types.begin(); }
1085  const_type_iterator types_end() const { return Types.end(); }
1086
1087  //===--------------------------------------------------------------------===//
1088  //                    Integer Values
1089  //===--------------------------------------------------------------------===//
1090
1091  /// MakeIntValue - Make an APSInt of the appropriate width and
1092  /// signedness for the given \arg Value and integer \arg Type.
1093  llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) {
1094    llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType());
1095    Res = Value;
1096    return Res;
1097  }
1098
1099  /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
1100  ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D);
1101  /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
1102  ObjCCategoryImplDecl   *getObjCImplementation(ObjCCategoryDecl *D);
1103
1104  /// \brief Set the implementation of ObjCInterfaceDecl.
1105  void setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1106                             ObjCImplementationDecl *ImplD);
1107  /// \brief Set the implementation of ObjCCategoryDecl.
1108  void setObjCImplementation(ObjCCategoryDecl *CatD,
1109                             ObjCCategoryImplDecl *ImplD);
1110
1111  /// \brief Allocate an uninitialized DeclaratorInfo.
1112  ///
1113  /// The caller should initialize the memory held by DeclaratorInfo using
1114  /// the TypeLoc wrappers.
1115  ///
1116  /// \param T the type that will be the basis for type source info. This type
1117  /// should refer to how the declarator was written in source code, not to
1118  /// what type semantic analysis resolved the declarator to.
1119  ///
1120  /// \param Size the size of the type info to create, or 0 if the size
1121  /// should be calculated based on the type.
1122  DeclaratorInfo *CreateDeclaratorInfo(QualType T, unsigned Size = 0);
1123
1124  /// \brief Allocate a DeclaratorInfo where all locations have been
1125  /// initialized to a given location, which defaults to the empty
1126  /// location.
1127  DeclaratorInfo *
1128  getTrivialDeclaratorInfo(QualType T, SourceLocation Loc = SourceLocation());
1129
1130private:
1131  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
1132  void operator=(const ASTContext&); // DO NOT IMPLEMENT
1133
1134  void InitBuiltinTypes();
1135  void InitBuiltinType(CanQualType &R, BuiltinType::Kind K);
1136
1137  // Return the ObjC type encoding for a given type.
1138  void getObjCEncodingForTypeImpl(QualType t, std::string &S,
1139                                  bool ExpandPointedToStructures,
1140                                  bool ExpandStructures,
1141                                  const FieldDecl *Field,
1142                                  bool OutermostType = false,
1143                                  bool EncodingProperty = false);
1144
1145  const ASTRecordLayout &getObjCLayout(const ObjCInterfaceDecl *D,
1146                                       const ObjCImplementationDecl *Impl);
1147};
1148
1149/// @brief Utility function for constructing a nullary selector.
1150static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
1151  IdentifierInfo* II = &Ctx.Idents.get(name);
1152  return Ctx.Selectors.getSelector(0, &II);
1153}
1154
1155/// @brief Utility function for constructing an unary selector.
1156static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
1157  IdentifierInfo* II = &Ctx.Idents.get(name);
1158  return Ctx.Selectors.getSelector(1, &II);
1159}
1160
1161}  // end namespace clang
1162
1163// operator new and delete aren't allowed inside namespaces.
1164// The throw specifications are mandated by the standard.
1165/// @brief Placement new for using the ASTContext's allocator.
1166///
1167/// This placement form of operator new uses the ASTContext's allocator for
1168/// obtaining memory. It is a non-throwing new, which means that it returns
1169/// null on error. (If that is what the allocator does. The current does, so if
1170/// this ever changes, this operator will have to be changed, too.)
1171/// Usage looks like this (assuming there's an ASTContext 'Context' in scope):
1172/// @code
1173/// // Default alignment (16)
1174/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
1175/// // Specific alignment
1176/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
1177/// @endcode
1178/// Please note that you cannot use delete on the pointer; it must be
1179/// deallocated using an explicit destructor call followed by
1180/// @c Context.Deallocate(Ptr).
1181///
1182/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
1183/// @param C The ASTContext that provides the allocator.
1184/// @param Alignment The alignment of the allocated memory (if the underlying
1185///                  allocator supports it).
1186/// @return The allocated memory. Could be NULL.
1187inline void *operator new(size_t Bytes, clang::ASTContext &C,
1188                          size_t Alignment) throw () {
1189  return C.Allocate(Bytes, Alignment);
1190}
1191/// @brief Placement delete companion to the new above.
1192///
1193/// This operator is just a companion to the new above. There is no way of
1194/// invoking it directly; see the new operator for more details. This operator
1195/// is called implicitly by the compiler if a placement new expression using
1196/// the ASTContext throws in the object constructor.
1197inline void operator delete(void *Ptr, clang::ASTContext &C, size_t)
1198              throw () {
1199  C.Deallocate(Ptr);
1200}
1201
1202/// This placement form of operator new[] uses the ASTContext's allocator for
1203/// obtaining memory. It is a non-throwing new[], which means that it returns
1204/// null on error.
1205/// Usage looks like this (assuming there's an ASTContext 'Context' in scope):
1206/// @code
1207/// // Default alignment (16)
1208/// char *data = new (Context) char[10];
1209/// // Specific alignment
1210/// char *data = new (Context, 8) char[10];
1211/// @endcode
1212/// Please note that you cannot use delete on the pointer; it must be
1213/// deallocated using an explicit destructor call followed by
1214/// @c Context.Deallocate(Ptr).
1215///
1216/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
1217/// @param C The ASTContext that provides the allocator.
1218/// @param Alignment The alignment of the allocated memory (if the underlying
1219///                  allocator supports it).
1220/// @return The allocated memory. Could be NULL.
1221inline void *operator new[](size_t Bytes, clang::ASTContext& C,
1222                            size_t Alignment = 16) throw () {
1223  return C.Allocate(Bytes, Alignment);
1224}
1225
1226/// @brief Placement delete[] companion to the new[] above.
1227///
1228/// This operator is just a companion to the new[] above. There is no way of
1229/// invoking it directly; see the new[] operator for more details. This operator
1230/// is called implicitly by the compiler if a placement new[] expression using
1231/// the ASTContext throws in the object constructor.
1232inline void operator delete[](void *Ptr, clang::ASTContext &C) throw () {
1233  C.Deallocate(Ptr);
1234}
1235
1236#endif
1237