ASTContext.h revision 77c9647cae939104c6cb2b6a4dd8ca859d2e5770
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  /// getTypedefType - Return the unique reference to the type for the
181  /// specified typename decl.
182  QualType getTypedefType(TypedefDecl *Decl);
183  QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl);
184
185  /// getObjCQualifiedInterfaceType - Return a
186  /// ObjCQualifiedInterfaceType type for the given interface decl and
187  /// the conforming protocol list.
188  QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
189             ObjCProtocolDecl **ProtocolList, unsigned NumProtocols);
190
191  /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a
192  /// given 'id' and conforming protocol list.
193  QualType getObjCQualifiedIdType(QualType idType,
194                                  ObjCProtocolDecl **ProtocolList,
195                                  unsigned NumProtocols);
196
197
198  /// getTypeOfType - GCC extension.
199  QualType getTypeOfExpr(Expr *e);
200  QualType getTypeOfType(QualType t);
201
202  /// getTagDeclType - Return the unique reference to the type for the
203  /// specified TagDecl (struct/union/class/enum) decl.
204  QualType getTagDeclType(TagDecl *Decl);
205
206  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
207  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
208  QualType getSizeType() const;
209
210  /// getWcharType - Return the unique type for "wchar_t" (C99 7.17), defined
211  /// in <stddef.h>. Wide strings require this (C99 6.4.5p5).
212  QualType getWcharType() const;
213
214  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
215  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
216  QualType getPointerDiffType() const;
217
218  // getCFConstantStringType - Return the C structure type used to represent
219  // constant CFStrings.
220  QualType getCFConstantStringType();
221
222  // This setter/getter represents the ObjC type for an NSConstantString.
223  void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl);
224  QualType getObjCConstantStringInterface() const {
225    return ObjCConstantStringType;
226  }
227
228  // Return the ObjC type encoding for a given type.
229  void getObjCEncodingForType(QualType t, std::string &S,
230                              llvm::SmallVector<const RecordType *, 8> &RT) const;
231
232  // Put the string version of type qualifiers into S.
233  void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
234                                       std::string &S) const;
235
236  /// getObjCEncodingForMethodDecl - Return the encoded type for this method
237  /// declaration.
238  void getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl, std::string &S);
239
240  /// getObjCEncodingTypeSize returns size of type for objective-c encoding
241  /// purpose.
242  int getObjCEncodingTypeSize(QualType t);
243
244  // This setter/getter repreents the ObjC 'id' type. It is setup lazily, by
245  // Sema.
246  void setObjCIdType(TypedefDecl *Decl);
247  QualType getObjCIdType() const { return ObjCIdType; }
248
249  void setObjCSelType(TypedefDecl *Decl);
250  QualType getObjCSelType() const { return ObjCSelType; }
251
252  void setObjCProtoType(QualType QT);
253  QualType getObjCProtoType() const { return ObjCProtoType; }
254
255  void setObjCClassType(TypedefDecl *Decl);
256  QualType getObjCClassType() const { return ObjCClassType; }
257
258  void setBuiltinVaListType(QualType T);
259  QualType getBuiltinVaListType() const { return BuiltinVaListType; }
260
261  //===--------------------------------------------------------------------===//
262  //                         Type Sizing and Analysis
263  //===--------------------------------------------------------------------===//
264
265  /// getTypeInfo - Get the size and alignment of the specified complete type in
266  /// bits.
267  std::pair<uint64_t, unsigned> getTypeInfo(QualType T);
268
269  /// getTypeSize - Return the size of the specified type, in bits.  This method
270  /// does not work on incomplete types.
271  uint64_t getTypeSize(QualType T) {
272    return getTypeInfo(T).first;
273  }
274
275  /// getTypeAlign - Return the alignment of the specified type, in bits.  This
276  /// method does not work on incomplete types.
277  unsigned getTypeAlign(QualType T) {
278    return getTypeInfo(T).second;
279  }
280
281  /// getASTRecordLayout - Get or compute information about the layout of the
282  /// specified record (struct/union/class), which indicates its size and field
283  /// position information.
284  const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D);
285
286  //===--------------------------------------------------------------------===//
287  //                            Type Operators
288  //===--------------------------------------------------------------------===//
289
290  /// getCanonicalType - Return the canonical (structural) type corresponding to
291  /// the specified potentially non-canonical type.  The non-canonical version
292  /// of a type may have many "decorated" versions of types.  Decorators can
293  /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
294  /// to be free of any of these, allowing two canonical types to be compared
295  /// for exact equality with a simple pointer comparison.
296  QualType getCanonicalType(QualType T);
297
298  /// getArrayDecayedType - Return the properly qualified result of decaying the
299  /// specified array type to a pointer.  This operation is non-trivial when
300  /// handling typedefs etc.  The canonical type of "T" must be an array type,
301  /// this returns a pointer to a properly qualified element of the array.
302  ///
303  /// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
304  QualType getArrayDecayedType(QualType T);
305
306  /// maxIntegerType - Returns the highest ranked integer type. Handles 3
307  /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned.
308  static QualType maxIntegerType(QualType lhs, QualType rhs);
309
310  /// compareFloatingType - Handles 3 different combos:
311  /// float/float, float/complex, complex/complex.
312  /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
313  static int compareFloatingType(QualType lt, QualType rt);
314
315  /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
316  /// point or a complex type (based on typeDomain/typeSize).
317  /// 'typeDomain' is a real floating point or complex type.
318  /// 'typeSize' is a real floating point or complex type.
319  QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
320                                             QualType typeDomain) const;
321
322  //===--------------------------------------------------------------------===//
323  //                    Type Compatibility Predicates
324  //===--------------------------------------------------------------------===//
325
326  /// Compatibility predicates used to check assignment expressions.
327  bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1
328  bool tagTypesAreCompatible(QualType, QualType); // C99 6.2.7p1
329  bool pointerTypesAreCompatible(QualType, QualType);  // C99 6.7.5.1p2
330  bool referenceTypesAreCompatible(QualType, QualType); // C++ 5.17p6
331  bool functionTypesAreCompatible(QualType, QualType); // C99 6.7.5.3p15
332  bool arrayTypesAreCompatible(QualType, QualType); // C99 6.7.5.2p6
333  bool builtinTypesAreCompatible(QualType, QualType);
334  bool vectorTypesAreCompatible(QualType, QualType);
335
336  bool QualifiedInterfaceTypesAreCompatible(QualType, QualType);
337  bool ObjCQualifiedIdTypesAreCompatible(QualType, QualType, bool = false);
338  bool objcTypesAreCompatible(QualType, QualType);
339  bool isObjCIdType(QualType T) const {
340    if (!IdStructType) // ObjC isn't enabled
341      return false;
342    return T->getAsStructureType() == IdStructType;
343  }
344  bool isObjCClassType(QualType T) const {
345    if (!ClassStructType) // ObjC isn't enabled
346      return false;
347    return T->getAsStructureType() == ClassStructType;
348  }
349  bool isObjCSelType(QualType T) const {
350    assert(SelStructType && "isObjCSelType used before 'SEL' type is built");
351    return T->getAsStructureType() == SelStructType;
352  }
353
354private:
355  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
356  void operator=(const ASTContext&); // DO NOT IMPLEMENT
357
358  void InitBuiltinTypes();
359  void InitBuiltinType(QualType &R, BuiltinType::Kind K);
360
361  /// helper function for Objective-C specific type checking.
362  bool interfaceTypesAreCompatible(QualType, QualType);
363
364  //===--------------------------------------------------------------------===//
365  //                    Serialization
366  //===--------------------------------------------------------------------===//
367
368public:
369  void Emit(llvm::Serializer& S) const;
370  static ASTContext* Create(llvm::Deserializer& D);
371};
372
373}  // end namespace clang
374
375#endif
376