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