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