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