ASTContext.h revision 2ce52f3fb95bf544db6bd3d91a72bce7d9cceb6c
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/AST/Builtins.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
20#include "clang/AST/Type.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/StringMap.h"
23#include "llvm/ADT/FoldingSet.h"
24#include "llvm/Bitcode/SerializationFwd.h"
25#include "llvm/Support/Allocator.h"
26#include <vector>
27
28namespace clang {
29  class TargetInfo;
30  class IdentifierTable;
31
32/// ASTContext - This class holds long-lived AST nodes (such as types and
33/// decls) that can be referred to throughout the semantic analysis of a file.
34class ASTContext {
35  std::vector<Type*> Types;
36  llvm::FoldingSet<ASQualType> ASQualTypes;
37  llvm::FoldingSet<ComplexType> ComplexTypes;
38  llvm::FoldingSet<PointerType> PointerTypes;
39  llvm::FoldingSet<ReferenceType> ReferenceTypes;
40  llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes;
41  llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes;
42  std::vector<VariableArrayType*> VariableArrayTypes;
43  llvm::FoldingSet<VectorType> VectorTypes;
44  llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos;
45  llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos;
46  llvm::FoldingSet<ObjCQualifiedInterfaceType> ObjCQualifiedInterfaceTypes;
47  llvm::FoldingSet<ObjCQualifiedIdType> ObjCQualifiedIdTypes;
48  /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts.
49  ///  This is lazily created.  This is intentionally not serialized.
50  llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts;
51
52  llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
53
54  /// BuiltinVaListType - built-in va list type.
55  /// This is initially null and set by Sema::LazilyCreateBuiltin when
56  /// a builtin that takes a valist is encountered.
57  QualType BuiltinVaListType;
58
59  /// ObjCIdType - a pseudo built-in typedef type (set by Sema).
60  QualType ObjCIdType;
61  const RecordType *IdStructType;
62
63  /// ObjCSelType - another pseudo built-in typedef type (set by Sema).
64  QualType ObjCSelType;
65  const RecordType *SelStructType;
66
67  /// ObjCProtoType - another pseudo built-in typedef type (set by Sema).
68  QualType ObjCProtoType;
69  const RecordType *ProtoStructType;
70
71  /// ObjCClassType - another pseudo built-in typedef type (set by Sema).
72  QualType ObjCClassType;
73  const RecordType *ClassStructType;
74
75  QualType ObjCConstantStringType;
76  RecordDecl *CFConstantStringTypeDecl;
77
78  SourceManager &SourceMgr;
79  llvm::MallocAllocator Allocator;
80public:
81  TargetInfo &Target;
82  IdentifierTable &Idents;
83  SelectorTable &Selectors;
84
85  SourceManager& getSourceManager() { return SourceMgr; }
86  llvm::MallocAllocator &getAllocator() { return Allocator; }
87
88  FullSourceLoc getFullLoc(SourceLocation Loc) const {
89    return FullSourceLoc(Loc,SourceMgr);
90  }
91
92  /// This is intentionally not serialized.  It is populated by the
93  /// ASTContext ctor, and there are no external pointers/references to
94  /// internal variables of BuiltinInfo.
95  Builtin::Context BuiltinInfo;
96
97  // Builtin Types.
98  QualType VoidTy;
99  QualType BoolTy;
100  QualType CharTy;
101  QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy;
102  QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
103  QualType UnsignedLongLongTy;
104  QualType FloatTy, DoubleTy, LongDoubleTy;
105  QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
106  QualType VoidPtrTy;
107
108  ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents,
109             SelectorTable &sels, unsigned size_reserve=0 ) :
110    CFConstantStringTypeDecl(0), SourceMgr(SM), Target(t),
111    Idents(idents), Selectors(sels) {
112
113    if (size_reserve > 0) Types.reserve(size_reserve);
114    InitBuiltinTypes();
115    BuiltinInfo.InitializeBuiltins(idents, Target);
116  }
117
118  ~ASTContext();
119
120  void PrintStats() const;
121  const std::vector<Type*>& getTypes() const { return Types; }
122
123  //===--------------------------------------------------------------------===//
124  //                           Type Constructors
125  //===--------------------------------------------------------------------===//
126
127  /// getASQualType - Return the uniqued reference to the type for an address
128  /// space qualified type with the specified type and address space.  The
129  /// resulting type has a union of the qualifiers from T and the address space.
130  // If T already has an address space specifier, it is silently replaced.
131  QualType getASQualType(QualType T, unsigned AddressSpace);
132
133  /// getComplexType - Return the uniqued reference to the type for a complex
134  /// number with the specified element type.
135  QualType getComplexType(QualType T);
136
137  /// getPointerType - Return the uniqued reference to the type for a pointer to
138  /// the specified type.
139  QualType getPointerType(QualType T);
140
141  /// getReferenceType - Return the uniqued reference to the type for a
142  /// reference to the specified type.
143  QualType getReferenceType(QualType T);
144
145  /// getVariableArrayType - Returns a non-unique reference to the type for a
146  /// variable array of the specified element type.
147  QualType getVariableArrayType(QualType EltTy, Expr *NumElts,
148                                ArrayType::ArraySizeModifier ASM,
149                                unsigned EltTypeQuals);
150
151  /// getIncompleteArrayType - Returns a unique reference to the type for a
152  /// incomplete array of the specified element type.
153  QualType getIncompleteArrayType(QualType EltTy,
154                                  ArrayType::ArraySizeModifier ASM,
155                                  unsigned EltTypeQuals);
156
157  /// getConstantArrayType - Return the unique reference to the type for a
158  /// constant array of the specified element type.
159  QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize,
160                                ArrayType::ArraySizeModifier ASM,
161                                unsigned EltTypeQuals);
162
163  /// getVectorType - Return the unique reference to a vector type of
164  /// the specified element type and size. VectorType must be a built-in type.
165  QualType getVectorType(QualType VectorType, unsigned NumElts);
166
167  /// getOCUVectorType - Return the unique reference to an OCU vector type of
168  /// the specified element type and size. VectorType must be a built-in type.
169  QualType getOCUVectorType(QualType VectorType, unsigned NumElts);
170
171  /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
172  ///
173  QualType getFunctionTypeNoProto(QualType ResultTy);
174
175  /// getFunctionType - Return a normal function type with a typed argument
176  /// list.  isVariadic indicates whether the argument list includes '...'.
177  QualType getFunctionType(QualType ResultTy, QualType *ArgArray,
178                           unsigned NumArgs, bool isVariadic);
179
180  /// getTypeDeclType - Return the unique reference to the type for
181  /// the specified type declaration.
182  QualType getTypeDeclType(TypeDecl *Decl);
183
184  /// getTypedefType - Return the unique reference to the type for the
185  /// specified typename decl.
186  QualType getTypedefType(TypedefDecl *Decl);
187  QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl);
188
189  /// getObjCQualifiedInterfaceType - Return a
190  /// ObjCQualifiedInterfaceType type for the given interface decl and
191  /// the conforming protocol list.
192  QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
193             ObjCProtocolDecl **ProtocolList, unsigned NumProtocols);
194
195  /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a
196  /// given 'id' and conforming protocol list.
197  QualType getObjCQualifiedIdType(QualType idType,
198                                  ObjCProtocolDecl **ProtocolList,
199                                  unsigned NumProtocols);
200
201
202  /// getTypeOfType - GCC extension.
203  QualType getTypeOfExpr(Expr *e);
204  QualType getTypeOfType(QualType t);
205
206  /// getTagDeclType - Return the unique reference to the type for the
207  /// specified TagDecl (struct/union/class/enum) decl.
208  QualType getTagDeclType(TagDecl *Decl);
209
210  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
211  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
212  QualType getSizeType() const;
213
214  /// getWcharType - Return the unique type for "wchar_t" (C99 7.17), defined
215  /// in <stddef.h>. Wide strings require this (C99 6.4.5p5).
216  QualType getWcharType() const;
217
218  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
219  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
220  QualType getPointerDiffType() const;
221
222  // getCFConstantStringType - Return the C structure type used to represent
223  // constant CFStrings.
224  QualType getCFConstantStringType();
225
226  // This setter/getter represents the ObjC type for an NSConstantString.
227  void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl);
228  QualType getObjCConstantStringInterface() const {
229    return ObjCConstantStringType;
230  }
231
232  // Return the ObjC type encoding for a given type.
233  void getObjCEncodingForType(QualType t, std::string &S,
234                              llvm::SmallVector<const RecordType *, 8> &RT) const;
235
236  // Put the string version of type qualifiers into S.
237  void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
238                                       std::string &S) const;
239
240  /// getObjCEncodingForMethodDecl - Return the encoded type for this method
241  /// declaration.
242  void getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl, std::string &S);
243
244  /// getObjCEncodingTypeSize returns size of type for objective-c encoding
245  /// purpose.
246  int getObjCEncodingTypeSize(QualType t);
247
248  // This setter/getter repreents the ObjC 'id' type. It is setup lazily, by
249  // Sema.
250  void setObjCIdType(TypedefDecl *Decl);
251  QualType getObjCIdType() const { return ObjCIdType; }
252
253  void setObjCSelType(TypedefDecl *Decl);
254  QualType getObjCSelType() const { return ObjCSelType; }
255
256  void setObjCProtoType(QualType QT);
257  QualType getObjCProtoType() const { return ObjCProtoType; }
258
259  void setObjCClassType(TypedefDecl *Decl);
260  QualType getObjCClassType() const { return ObjCClassType; }
261
262  void setBuiltinVaListType(QualType T);
263  QualType getBuiltinVaListType() const { return BuiltinVaListType; }
264
265  //===--------------------------------------------------------------------===//
266  //                         Type Sizing and Analysis
267  //===--------------------------------------------------------------------===//
268
269  /// getTypeInfo - Get the size and alignment of the specified complete type in
270  /// bits.
271  std::pair<uint64_t, unsigned> getTypeInfo(QualType T);
272
273  /// getTypeSize - Return the size of the specified type, in bits.  This method
274  /// does not work on incomplete types.
275  uint64_t getTypeSize(QualType T) {
276    return getTypeInfo(T).first;
277  }
278
279  /// getTypeAlign - Return the alignment of the specified type, in bits.  This
280  /// method does not work on incomplete types.
281  unsigned getTypeAlign(QualType T) {
282    return getTypeInfo(T).second;
283  }
284
285  /// getASTRecordLayout - Get or compute information about the layout of the
286  /// specified record (struct/union/class), which indicates its size and field
287  /// position information.
288  const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D);
289
290  //===--------------------------------------------------------------------===//
291  //                            Type Operators
292  //===--------------------------------------------------------------------===//
293
294  /// getCanonicalType - Return the canonical (structural) type corresponding to
295  /// the specified potentially non-canonical type.  The non-canonical version
296  /// of a type may have many "decorated" versions of types.  Decorators can
297  /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
298  /// to be free of any of these, allowing two canonical types to be compared
299  /// for exact equality with a simple pointer comparison.
300  QualType getCanonicalType(QualType T);
301
302  /// getArrayDecayedType - Return the properly qualified result of decaying the
303  /// specified array type to a pointer.  This operation is non-trivial when
304  /// handling typedefs etc.  The canonical type of "T" must be an array type,
305  /// this returns a pointer to a properly qualified element of the array.
306  ///
307  /// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
308  QualType getArrayDecayedType(QualType T);
309
310  /// getIntegerTypeOrder - Returns the highest ranked integer type:
311  /// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
312  /// LHS < RHS, return -1.
313  int getIntegerTypeOrder(QualType LHS, QualType RHS);
314
315  /// getFloatingTypeOrder - Compare the rank of the two specified floating
316  /// point types, ignoring the domain of the type (i.e. 'double' ==
317  /// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
318  /// LHS < RHS, return -1.
319  int getFloatingTypeOrder(QualType LHS, QualType RHS);
320
321  /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
322  /// point or a complex type (based on typeDomain/typeSize).
323  /// 'typeDomain' is a real floating point or complex type.
324  /// 'typeSize' is a real floating point or complex type.
325  QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
326                                             QualType typeDomain) const;
327
328  //===--------------------------------------------------------------------===//
329  //                    Type Compatibility Predicates
330  //===--------------------------------------------------------------------===//
331
332  /// Compatibility predicates used to check assignment expressions.
333  bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1
334  bool pointerTypesAreCompatible(QualType, QualType);  // C99 6.7.5.1p2
335  bool referenceTypesAreCompatible(QualType, QualType); // C++ 5.17p6
336  bool functionTypesAreCompatible(QualType, QualType); // C99 6.7.5.3p15
337
338  bool isObjCIdType(QualType T) const {
339    if (!IdStructType) // ObjC isn't enabled
340      return false;
341    return T->getAsStructureType() == IdStructType;
342  }
343  bool isObjCClassType(QualType T) const {
344    if (!ClassStructType) // ObjC isn't enabled
345      return false;
346    return T->getAsStructureType() == ClassStructType;
347  }
348  bool isObjCSelType(QualType T) const {
349    assert(SelStructType && "isObjCSelType used before 'SEL' type is built");
350    return T->getAsStructureType() == SelStructType;
351  }
352
353  //===--------------------------------------------------------------------===//
354  //                    Serialization
355  //===--------------------------------------------------------------------===//
356
357  void Emit(llvm::Serializer& S) const;
358  static ASTContext* Create(llvm::Deserializer& D);
359
360private:
361  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
362  void operator=(const ASTContext&); // DO NOT IMPLEMENT
363
364  void InitBuiltinTypes();
365  void InitBuiltinType(QualType &R, BuiltinType::Kind K);
366};
367
368}  // end namespace clang
369
370#endif
371