ASTContext.h revision a9a4a24592a2164114a8a36717650e6341eb67a4
1//===--- ASTContext.h - Context to hold long-lived AST nodes ----*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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
46  /// RecordLayoutInfo - A cache mapping from RecordDecls to RecordLayoutInfo.
47  ///  This is lazily created.  This is intentionally not serialized.
48  llvm::DenseMap<const RecordDecl*, const RecordLayout*> RecordLayoutInfo;
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;
73public:
74
75  SourceManager &SourceMgr;
76  TargetInfo &Target;
77  IdentifierTable &Idents;
78  SelectorTable &Selectors;
79
80  /// This is intentionally not serialized.  It is populated by the
81  /// ASTContext ctor, and there are no external pointers/references to
82  /// internal variables of BuiltinInfo.
83  Builtin::Context BuiltinInfo;
84
85  // Builtin Types.
86  QualType VoidTy;
87  QualType BoolTy;
88  QualType CharTy;
89  QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy;
90  QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
91  QualType UnsignedLongLongTy;
92  QualType FloatTy, DoubleTy, LongDoubleTy;
93  QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
94  QualType VoidPtrTy;
95
96  ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents,
97             SelectorTable &sels, unsigned size_reserve=0 ) :
98    CFConstantStringTypeDecl(0), SourceMgr(SM), Target(t),
99    Idents(idents), Selectors(sels) {
100
101    if (size_reserve > 0) Types.reserve(size_reserve);
102    InitBuiltinTypes();
103    BuiltinInfo.InitializeBuiltins(idents, Target);
104  }
105
106  ~ASTContext();
107
108  void PrintStats() const;
109
110  //===--------------------------------------------------------------------===//
111  //                           Type Constructors
112  //===--------------------------------------------------------------------===//
113
114  /// getComplexType - Return the uniqued reference to the type for a complex
115  /// number with the specified element type.
116  QualType getComplexType(QualType T);
117
118  /// getPointerType - Return the uniqued reference to the type for a pointer to
119  /// the specified type.
120  QualType getPointerType(QualType T);
121
122  /// getReferenceType - Return the uniqued reference to the type for a
123  /// reference to the specified type.
124  QualType getReferenceType(QualType T);
125
126  /// getVariableArrayType - Returns a non-unique reference to the type for a
127  /// variable array of the specified element type.
128  QualType getVariableArrayType(QualType EltTy, Expr *NumElts,
129                                ArrayType::ArraySizeModifier ASM,
130                                unsigned EltTypeQuals);
131
132  /// getConstantArrayType - Return the unique reference to the type for a
133  /// constant array of the specified element type.
134  QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize,
135                                ArrayType::ArraySizeModifier ASM,
136                                unsigned EltTypeQuals);
137
138  /// getVectorType - Return the unique reference to a vector type of
139  /// the specified element type and size. VectorType must be a built-in type.
140  QualType getVectorType(QualType VectorType, unsigned NumElts);
141
142  /// getOCUVectorType - Return the unique reference to an OCU vector type of
143  /// the specified element type and size. VectorType must be a built-in type.
144  QualType getOCUVectorType(QualType VectorType, unsigned NumElts);
145
146  /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
147  ///
148  QualType getFunctionTypeNoProto(QualType ResultTy);
149
150  /// getFunctionType - Return a normal function type with a typed argument
151  /// list.  isVariadic indicates whether the argument list includes '...'.
152  QualType getFunctionType(QualType ResultTy, QualType *ArgArray,
153                           unsigned NumArgs, bool isVariadic);
154
155  /// getTypedefType - Return the unique reference to the type for the
156  /// specified typename decl.
157  QualType getTypedefType(TypedefDecl *Decl);
158  QualType getObjcInterfaceType(ObjcInterfaceDecl *Decl);
159
160  /// getObjcQualifiedInterfaceType - Return a
161  /// ObjcQualifiedInterfaceType type for the given interface decl and
162  /// the conforming protocol list.
163  QualType getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
164             ObjcProtocolDecl **ProtocolList, unsigned NumProtocols);
165
166  /// getTypeOfType - GCC extension.
167  QualType getTypeOfExpr(Expr *e);
168  QualType getTypeOfType(QualType t);
169
170  /// getTagDeclType - Return the unique reference to the type for the
171  /// specified TagDecl (struct/union/class/enum) decl.
172  QualType getTagDeclType(TagDecl *Decl);
173
174  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
175  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
176  QualType getSizeType() const;
177
178  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
179  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
180  QualType getPointerDiffType() const;
181
182  // getCFConstantStringType - Return the type used for constant CFStrings.
183  // CURRENTLY UNUSED (10/15/07). ObjCStringLiteral now uses the hook below.
184  QualType getCFConstantStringType();
185
186  // This setter/getter represents the ObjC type for an NSConstantString.
187  void setObjcConstantStringInterface(ObjcInterfaceDecl *Decl);
188  QualType getObjcConstantStringInterface() const {
189    return ObjcConstantStringType;
190  }
191
192  // Return the ObjC type encoding for a given type.
193  void getObjcEncodingForType(QualType t, std::string &S) const;
194
195  // Put the string version of type qualifiers into S.
196  void getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT,
197                                       std::string &S) const;
198
199  /// getObjcEncodingForMethodDecl - Return the encoded type for this method
200  /// declaration.
201  void getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl, std::string &S);
202
203  /// getObjcEncodingTypeSize returns size of type for objective-c encoding
204  /// purpose.
205  int getObjcEncodingTypeSize(QualType t);
206
207  // This setter/getter repreents the ObjC 'id' type. It is setup lazily, by
208  // Sema.
209  void setObjcIdType(TypedefDecl *Decl);
210  QualType getObjcIdType() const { return ObjcIdType; }
211
212  void setObjcSelType(TypedefDecl *Decl);
213  QualType getObjcSelType() const { return ObjcSelType; }
214
215  void setObjcProtoType(TypedefDecl *Decl);
216  QualType getObjcProtoType() const { return ObjcProtoType; }
217
218  void setObjcClassType(TypedefDecl *Decl);
219  QualType getObjcClassType() const { return ObjcClassType; }
220
221  void setBuiltinVaListType(QualType T);
222  QualType getBuiltinVaListType() const { return BuiltinVaListType; }
223
224  //===--------------------------------------------------------------------===//
225  //                         Type Sizing and Analysis
226  //===--------------------------------------------------------------------===//
227
228  /// getTypeInfo - Get the size and alignment of the specified complete type in
229  /// bits.
230  std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L);
231
232  /// getTypeSize - Return the size of the specified type, in bits.  This method
233  /// does not work on incomplete types.
234  uint64_t getTypeSize(QualType T, SourceLocation L) {
235    return getTypeInfo(T, L).first;
236  }
237
238  /// getTypeAlign - Return the alignment of the specified type, in bits.  This
239  /// method does not work on incomplete types.
240  unsigned getTypeAlign(QualType T, SourceLocation L) {
241    return getTypeInfo(T, L).second;
242  }
243
244  /// getRecordLayout - Get or compute information about the layout of the
245  /// specified record (struct/union/class), which indicates its size and field
246  /// position information.
247  const RecordLayout &getRecordLayout(const RecordDecl *D, SourceLocation L);
248
249  //===--------------------------------------------------------------------===//
250  //                            Type Operators
251  //===--------------------------------------------------------------------===//
252
253  /// maxIntegerType - Returns the highest ranked integer type. Handles 3
254  /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned.
255  static QualType maxIntegerType(QualType lhs, QualType rhs);
256
257  /// compareFloatingType - Handles 3 different combos:
258  /// float/float, float/complex, complex/complex.
259  /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
260  static int compareFloatingType(QualType lt, QualType rt);
261
262  /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
263  /// point or a complex type (based on typeDomain/typeSize).
264  /// 'typeDomain' is a real floating point or complex type.
265  /// 'typeSize' is a real floating point or complex type.
266  QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
267                                             QualType typeDomain) const;
268
269  //===--------------------------------------------------------------------===//
270  //                    Type Compatibility Predicates
271  //===--------------------------------------------------------------------===//
272
273  /// Compatibility predicates used to check assignment expressions.
274  bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1
275  bool tagTypesAreCompatible(QualType, QualType); // C99 6.2.7p1
276  bool pointerTypesAreCompatible(QualType, QualType);  // C99 6.7.5.1p2
277  bool referenceTypesAreCompatible(QualType, QualType); // C++ 5.17p6
278  bool functionTypesAreCompatible(QualType, QualType); // C99 6.7.5.3p15
279  bool arrayTypesAreCompatible(QualType, QualType); // C99 6.7.5.2p6
280  bool builtinTypesAreCompatible(QualType, QualType);
281  bool vectorTypesAreCompatible(QualType, QualType);
282
283  /// Objective-C specific type checking.
284  bool interfaceTypesAreCompatible(QualType, QualType);
285  bool objcTypesAreCompatible(QualType, QualType);
286  bool isObjcIdType(QualType T) const {
287    assert(IdStructType && "isObjcIdType used before 'id' type is built");
288    return T->getAsStructureType() == IdStructType;
289  }
290  bool isObjcClassType(QualType T) const {
291    assert(ClassStructType && "isObjcClassType used before 'Class' type is built");
292    return T->getAsStructureType() == ClassStructType;
293  }
294  bool isObjcSelType(QualType T) const {
295    assert(SelStructType && "isObjcSelType used before 'SEL' type is built");
296    return T->getAsStructureType() == SelStructType;
297  }
298
299private:
300  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
301  void operator=(const ASTContext&); // DO NOT IMPLEMENT
302
303  void InitBuiltinTypes();
304  void InitBuiltinType(QualType &R, BuiltinType::Kind K);
305
306  //===--------------------------------------------------------------------===//
307  //                    Serialization
308  //===--------------------------------------------------------------------===//
309
310  void Emit(llvm::Serializer& S) const;
311  static ASTContext* Materialize(llvm::Deserializer& D);
312};
313
314}  // end namespace clang
315
316#endif
317