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