ASTContext.h revision a769c004a2874504c17ea8afccbc4ad35fc33c9f
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/AST/Builtins.h"
20#include "clang/AST/DeclarationName.h"
21#include "clang/AST/DeclBase.h"
22#include "clang/AST/Type.h"
23#include "clang/Basic/SourceLocation.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/FoldingSet.h"
26#include "llvm/Bitcode/SerializationFwd.h"
27#include "llvm/Support/Allocator.h"
28#include <vector>
29
30namespace llvm {
31  struct fltSemantics;
32}
33
34namespace clang {
35  class ASTRecordLayout;
36  class Expr;
37  class IdentifierTable;
38  class SelectorTable;
39  class SourceManager;
40  class TargetInfo;
41  // Decls
42  class Decl;
43  class ObjCPropertyDecl;
44  class RecordDecl;
45  class TagDecl;
46  class TranslationUnitDecl;
47  class TypeDecl;
48  class TypedefDecl;
49  class TemplateTypeParmDecl;
50
51/// ASTContext - This class holds long-lived AST nodes (such as types and
52/// decls) that can be referred to throughout the semantic analysis of a file.
53class ASTContext {
54  std::vector<Type*> Types;
55  llvm::FoldingSet<ASQualType> ASQualTypes;
56  llvm::FoldingSet<ComplexType> ComplexTypes;
57  llvm::FoldingSet<PointerType> PointerTypes;
58  llvm::FoldingSet<BlockPointerType> BlockPointerTypes;
59  llvm::FoldingSet<ReferenceType> ReferenceTypes;
60  llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes;
61  llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes;
62  std::vector<VariableArrayType*> VariableArrayTypes;
63  std::vector<DependentSizedArrayType*> DependentSizedArrayTypes;
64  llvm::FoldingSet<VectorType> VectorTypes;
65  llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos;
66  llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos;
67  llvm::FoldingSet<ObjCQualifiedInterfaceType> ObjCQualifiedInterfaceTypes;
68  llvm::FoldingSet<ObjCQualifiedIdType> ObjCQualifiedIdTypes;
69  /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts.
70  ///  This is lazily created.  This is intentionally not serialized.
71  llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts;
72  llvm::DenseMap<const ObjCInterfaceDecl*,
73                 const ASTRecordLayout*> ASTObjCInterfaces;
74
75  llvm::DenseMap<const ObjCInterfaceDecl*,
76                 const RecordDecl*> ASTRecordForInterface;
77
78  /// BuiltinVaListType - built-in va list type.
79  /// This is initially null and set by Sema::LazilyCreateBuiltin when
80  /// a builtin that takes a valist is encountered.
81  QualType BuiltinVaListType;
82
83  /// ObjCIdType - a pseudo built-in typedef type (set by Sema).
84  QualType ObjCIdType;
85  const RecordType *IdStructType;
86
87  /// ObjCSelType - another pseudo built-in typedef type (set by Sema).
88  QualType ObjCSelType;
89  const RecordType *SelStructType;
90
91  /// ObjCProtoType - another pseudo built-in typedef type (set by Sema).
92  QualType ObjCProtoType;
93  const RecordType *ProtoStructType;
94
95  /// ObjCClassType - another pseudo built-in typedef type (set by Sema).
96  QualType ObjCClassType;
97  const RecordType *ClassStructType;
98
99  QualType ObjCConstantStringType;
100  RecordDecl *CFConstantStringTypeDecl;
101
102  RecordDecl *ObjCFastEnumerationStateTypeDecl;
103
104  TranslationUnitDecl *TUDecl;
105
106  /// SourceMgr - The associated SourceManager object.
107  SourceManager &SourceMgr;
108
109  /// LangOpts - The language options used to create the AST associated with
110  ///  this ASTContext object.
111  LangOptions LangOpts;
112
113  /// Allocator - The allocator object used to create AST objects.
114  llvm::MallocAllocator Allocator;
115
116public:
117  TargetInfo &Target;
118  IdentifierTable &Idents;
119  SelectorTable &Selectors;
120  DeclarationNameTable DeclarationNames;
121
122  SourceManager& getSourceManager() { return SourceMgr; }
123  llvm::MallocAllocator &getAllocator() { return Allocator; }
124  const LangOptions& getLangOptions() const { return LangOpts; }
125
126  FullSourceLoc getFullLoc(SourceLocation Loc) const {
127    return FullSourceLoc(Loc,SourceMgr);
128  }
129
130  TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; }
131
132  /// This is intentionally not serialized.  It is populated by the
133  /// ASTContext ctor, and there are no external pointers/references to
134  /// internal variables of BuiltinInfo.
135  Builtin::Context BuiltinInfo;
136
137  // Builtin Types.
138  QualType VoidTy;
139  QualType BoolTy;
140  QualType CharTy;
141  QualType WCharTy; // [C++ 3.9.1p5]
142  QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy;
143  QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
144  QualType UnsignedLongLongTy;
145  QualType FloatTy, DoubleTy, LongDoubleTy;
146  QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
147  QualType VoidPtrTy;
148  QualType OverloadTy;
149  QualType DependentTy;
150
151  ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t,
152             IdentifierTable &idents, SelectorTable &sels,
153             unsigned size_reserve=0);
154
155  ~ASTContext();
156
157  void PrintStats() const;
158  const std::vector<Type*>& getTypes() const { return Types; }
159
160  //===--------------------------------------------------------------------===//
161  //                           Type Constructors
162  //===--------------------------------------------------------------------===//
163
164  /// getASQualType - Return the uniqued reference to the type for an address
165  /// space qualified type with the specified type and address space.  The
166  /// resulting type has a union of the qualifiers from T and the address space.
167  // If T already has an address space specifier, it is silently replaced.
168  QualType getASQualType(QualType T, unsigned AddressSpace);
169
170  /// getComplexType - Return the uniqued reference to the type for a complex
171  /// number with the specified element type.
172  QualType getComplexType(QualType T);
173
174  /// getPointerType - Return the uniqued reference to the type for a pointer to
175  /// the specified type.
176  QualType getPointerType(QualType T);
177
178  /// getBlockPointerType - Return the uniqued reference to the type for a block
179  /// of the specified type.
180  QualType getBlockPointerType(QualType T);
181
182  /// getReferenceType - Return the uniqued reference to the type for a
183  /// reference to the specified type.
184  QualType getReferenceType(QualType T);
185
186  /// getVariableArrayType - Returns a non-unique reference to the type for a
187  /// variable array of the specified element type.
188  QualType getVariableArrayType(QualType EltTy, Expr *NumElts,
189                                ArrayType::ArraySizeModifier ASM,
190                                unsigned EltTypeQuals);
191
192  /// getDependentSizedArrayType - Returns a non-unique reference to
193  /// the type for a dependently-sized array of the specified element
194  /// type. FIXME: We will need these to be uniqued, or at least
195  /// comparable, at some point.
196  QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
197                                      ArrayType::ArraySizeModifier ASM,
198                                      unsigned EltTypeQuals);
199
200  /// getIncompleteArrayType - Returns a unique reference to the type for a
201  /// incomplete array of the specified element type.
202  QualType getIncompleteArrayType(QualType EltTy,
203                                  ArrayType::ArraySizeModifier ASM,
204                                  unsigned EltTypeQuals);
205
206  /// getConstantArrayType - Return the unique reference to the type for a
207  /// constant array of the specified element type.
208  QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize,
209                                ArrayType::ArraySizeModifier ASM,
210                                unsigned EltTypeQuals);
211
212  /// getVectorType - Return the unique reference to a vector type of
213  /// the specified element type and size. VectorType must be a built-in type.
214  QualType getVectorType(QualType VectorType, unsigned NumElts);
215
216  /// getExtVectorType - Return the unique reference to an extended vector type
217  /// of the specified element type and size.  VectorType must be a built-in
218  /// type.
219  QualType getExtVectorType(QualType VectorType, unsigned NumElts);
220
221  /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
222  ///
223  QualType getFunctionTypeNoProto(QualType ResultTy);
224
225  /// getFunctionType - Return a normal function type with a typed argument
226  /// list.  isVariadic indicates whether the argument list includes '...'.
227  QualType getFunctionType(QualType ResultTy, const QualType *ArgArray,
228                           unsigned NumArgs, bool isVariadic,
229                           unsigned TypeQuals);
230
231  /// getTypeDeclType - Return the unique reference to the type for
232  /// the specified type declaration.
233  QualType getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl=0);
234
235  /// getTypedefType - Return the unique reference to the type for the
236  /// specified typename decl.
237  QualType getTypedefType(TypedefDecl *Decl);
238  QualType getTemplateTypeParmType(TemplateTypeParmDecl *Decl);
239  QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl);
240
241  /// getObjCQualifiedInterfaceType - Return a
242  /// ObjCQualifiedInterfaceType type for the given interface decl and
243  /// the conforming protocol list.
244  QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
245             ObjCProtocolDecl **ProtocolList, unsigned NumProtocols);
246
247  /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a
248  /// given 'id' and conforming protocol list.
249  QualType getObjCQualifiedIdType(ObjCProtocolDecl **ProtocolList,
250                                  unsigned NumProtocols);
251
252
253  /// getTypeOfType - GCC extension.
254  QualType getTypeOfExpr(Expr *e);
255  QualType getTypeOfType(QualType t);
256
257  /// getTagDeclType - Return the unique reference to the type for the
258  /// specified TagDecl (struct/union/class/enum) decl.
259  QualType getTagDeclType(TagDecl *Decl);
260
261  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
262  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
263  QualType getSizeType() const;
264
265  /// getWCharType - Return the unique type for "wchar_t" (C99 7.17), defined
266  /// in <stddef.h>. Wide strings require this (C99 6.4.5p5).
267  QualType getWCharType() const;
268
269  /// getSignedWCharType - Return the type of "signed wchar_t".
270  /// Used when in C++, as a GCC extension.
271  QualType getSignedWCharType() const;
272
273  /// getUnsignedWCharType - Return the type of "unsigned wchar_t".
274  /// Used when in C++, as a GCC extension.
275  QualType getUnsignedWCharType() const;
276
277  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
278  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
279  QualType getPointerDiffType() const;
280
281  // getCFConstantStringType - Return the C structure type used to represent
282  // constant CFStrings.
283  QualType getCFConstantStringType();
284
285  // This setter/getter represents the ObjC type for an NSConstantString.
286  void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl);
287  QualType getObjCConstantStringInterface() const {
288    return ObjCConstantStringType;
289  }
290
291  //// This gets the struct used to keep track of fast enumerations.
292  QualType getObjCFastEnumerationStateType();
293
294  /// getObjCEncodingForType - Emit the ObjC type encoding for the
295  /// given type into \arg S. If \arg NameFields is specified then
296  /// record field names are also encoded.
297  void getObjCEncodingForType(QualType t, std::string &S,
298                              bool NameFields=false) const;
299
300  // Put the string version of type qualifiers into S.
301  void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
302                                       std::string &S) const;
303
304  /// getObjCEncodingForMethodDecl - Return the encoded type for this method
305  /// declaration.
306  void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S);
307
308  /// getObjCEncodingForPropertyDecl - Return the encoded type for
309  /// this method declaration. If non-NULL, Container must be either
310  /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should
311  /// only be NULL when getting encodings for protocol properties.
312  void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
313                                      const Decl *Container,
314                                      std::string &S);
315
316  /// getObjCEncodingTypeSize returns size of type for objective-c encoding
317  /// purpose.
318  int getObjCEncodingTypeSize(QualType t);
319
320  /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by
321  /// Sema.  id is always a (typedef for a) pointer type, a pointer to a struct.
322  QualType getObjCIdType() const { return ObjCIdType; }
323  void setObjCIdType(TypedefDecl *Decl);
324
325  void setObjCSelType(TypedefDecl *Decl);
326  QualType getObjCSelType() const { return ObjCSelType; }
327
328  void setObjCProtoType(QualType QT);
329  QualType getObjCProtoType() const { return ObjCProtoType; }
330
331  /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by
332  /// Sema.  'Class' is always a (typedef for a) pointer type, a pointer to a
333  /// struct.
334  QualType getObjCClassType() const { return ObjCClassType; }
335  void setObjCClassType(TypedefDecl *Decl);
336
337  void setBuiltinVaListType(QualType T);
338  QualType getBuiltinVaListType() const { return BuiltinVaListType; }
339
340private:
341  QualType getFromTargetType(unsigned Type) const;
342
343  //===--------------------------------------------------------------------===//
344  //                         Type Predicates.
345  //===--------------------------------------------------------------------===//
346
347public:
348  /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
349  /// to an object type.  This includes "id" and "Class" (two 'special' pointers
350  /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
351  /// ID type).
352  bool isObjCObjectPointerType(QualType Ty) const;
353
354  //===--------------------------------------------------------------------===//
355  //                         Type Sizing and Analysis
356  //===--------------------------------------------------------------------===//
357
358  /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
359  /// scalar floating point type.
360  const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const;
361
362  /// getTypeInfo - Get the size and alignment of the specified complete type in
363  /// bits.
364  std::pair<uint64_t, unsigned> getTypeInfo(const Type *T);
365  std::pair<uint64_t, unsigned> getTypeInfo(QualType T) {
366    return getTypeInfo(T.getTypePtr());
367  }
368
369  /// getTypeSize - Return the size of the specified type, in bits.  This method
370  /// does not work on incomplete types.
371  uint64_t getTypeSize(QualType T) {
372    return getTypeInfo(T).first;
373  }
374  uint64_t getTypeSize(const Type *T) {
375    return getTypeInfo(T).first;
376  }
377
378  /// getTypeAlign - Return the alignment of the specified type, in bits.  This
379  /// method does not work on incomplete types.
380  unsigned getTypeAlign(QualType T) {
381    return getTypeInfo(T).second;
382  }
383  unsigned getTypeAlign(const Type *T) {
384    return getTypeInfo(T).second;
385  }
386
387  /// getASTRecordLayout - Get or compute information about the layout of the
388  /// specified record (struct/union/class), which indicates its size and field
389  /// position information.
390  const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D);
391
392  const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D);
393  const RecordDecl *addRecordToClass(const ObjCInterfaceDecl *D);
394  //===--------------------------------------------------------------------===//
395  //                            Type Operators
396  //===--------------------------------------------------------------------===//
397
398  /// getCanonicalType - Return the canonical (structural) type corresponding to
399  /// the specified potentially non-canonical type.  The non-canonical version
400  /// of a type may have many "decorated" versions of types.  Decorators can
401  /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
402  /// to be free of any of these, allowing two canonical types to be compared
403  /// for exact equality with a simple pointer comparison.
404  QualType getCanonicalType(QualType T);
405  const Type *getCanonicalType(const Type *T) {
406    return T->getCanonicalTypeInternal().getTypePtr();
407  }
408
409  /// Type Query functions.  If the type is an instance of the specified class,
410  /// return the Type pointer for the underlying maximally pretty type.  This
411  /// is a member of ASTContext because this may need to do some amount of
412  /// canonicalization, e.g. to move type qualifiers into the element type.
413  const ArrayType *getAsArrayType(QualType T);
414  const ConstantArrayType *getAsConstantArrayType(QualType T) {
415    return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T));
416  }
417  const VariableArrayType *getAsVariableArrayType(QualType T) {
418    return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T));
419  }
420  const IncompleteArrayType *getAsIncompleteArrayType(QualType T) {
421    return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T));
422  }
423
424  /// getArrayDecayedType - Return the properly qualified result of decaying the
425  /// specified array type to a pointer.  This operation is non-trivial when
426  /// handling typedefs etc.  The canonical type of "T" must be an array type,
427  /// this returns a pointer to a properly qualified element of the array.
428  ///
429  /// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
430  QualType getArrayDecayedType(QualType T);
431
432  /// getIntegerTypeOrder - Returns the highest ranked integer type:
433  /// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
434  /// LHS < RHS, return -1.
435  int getIntegerTypeOrder(QualType LHS, QualType RHS);
436
437  /// getFloatingTypeOrder - Compare the rank of the two specified floating
438  /// point types, ignoring the domain of the type (i.e. 'double' ==
439  /// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
440  /// LHS < RHS, return -1.
441  int getFloatingTypeOrder(QualType LHS, QualType RHS);
442
443  /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
444  /// point or a complex type (based on typeDomain/typeSize).
445  /// 'typeDomain' is a real floating point or complex type.
446  /// 'typeSize' is a real floating point or complex type.
447  QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
448                                             QualType typeDomain) const;
449
450  //===--------------------------------------------------------------------===//
451  //                    Type Compatibility Predicates
452  //===--------------------------------------------------------------------===//
453
454  /// Compatibility predicates used to check assignment expressions.
455  bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1
456  bool typesAreBlockCompatible(QualType lhs, QualType rhs);
457
458  bool isObjCIdType(QualType T) const {
459    if (!IdStructType) // ObjC isn't enabled
460      return false;
461    return T->getAsStructureType() == IdStructType;
462  }
463  bool isObjCClassType(QualType T) const {
464    if (!ClassStructType) // ObjC isn't enabled
465      return false;
466    return T->getAsStructureType() == ClassStructType;
467  }
468  bool isObjCSelType(QualType T) const {
469    assert(SelStructType && "isObjCSelType used before 'SEL' type is built");
470    return T->getAsStructureType() == SelStructType;
471  }
472
473  // Check the safety of assignment from LHS to RHS
474  bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
475                               const ObjCInterfaceType *RHS);
476
477  // Functions for calculating composite types
478  QualType mergeTypes(QualType, QualType);
479  QualType mergeFunctionTypes(QualType, QualType);
480
481  //===--------------------------------------------------------------------===//
482  //                    Integer Predicates
483  //===--------------------------------------------------------------------===//
484
485  // The width of an integer, as defined in C99 6.2.6.2. This is the number
486  // of bits in an integer type excluding any padding bits.
487  unsigned getIntWidth(QualType T);
488
489  // Per C99 6.2.5p6, for every signed integer type, there is a corresponding
490  // unsigned integer type.  This method takes a signed type, and returns the
491  // corresponding unsigned integer type.
492  QualType getCorrespondingUnsignedType(QualType T);
493
494  //===--------------------------------------------------------------------===//
495  //                    Type Iterators.
496  //===--------------------------------------------------------------------===//
497
498  typedef std::vector<Type*>::iterator       type_iterator;
499  typedef std::vector<Type*>::const_iterator const_type_iterator;
500
501  type_iterator types_begin() { return Types.begin(); }
502  type_iterator types_end() { return Types.end(); }
503  const_type_iterator types_begin() const { return Types.begin(); }
504  const_type_iterator types_end() const { return Types.end(); }
505
506  //===--------------------------------------------------------------------===//
507  //                    Serialization
508  //===--------------------------------------------------------------------===//
509
510  void Emit(llvm::Serializer& S) const;
511  static ASTContext* Create(llvm::Deserializer& D);
512
513private:
514  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
515  void operator=(const ASTContext&); // DO NOT IMPLEMENT
516
517  void InitBuiltinTypes();
518  void InitBuiltinType(QualType &R, BuiltinType::Kind K);
519
520  /// setTagDefinition - Used by RecordDecl::completeDefinition and
521  /// EnumDecl::completeDefinition to inform
522  /// about which RecordDecl/EnumDecl serves as the definition of a particular
523  /// struct/union/class/enum.
524  void setTagDefinition(TagDecl* R);
525  friend class EnumDecl;
526  friend class RecordDecl;
527
528  // Return the ObjC type encoding for a given type.
529  void getObjCEncodingForTypeImpl(QualType t, std::string &S,
530                                  bool ExpandPointedToStructures,
531                                  bool ExpandStructures,
532                                  bool NameFields) const;
533
534};
535
536}  // end namespace clang
537
538#endif
539