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