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