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