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