ASTContext.h revision 1d75118af76cae2bfc06389cde410e14bd0a19fc
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/Basic/LangOptions.h"
18#include "clang/AST/Builtins.h"
19#include "clang/AST/DeclBase.h"
20#include "clang/AST/Type.h"
21#include "clang/Basic/SourceLocation.h"
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/ADT/FoldingSet.h"
24#include "llvm/Bitcode/SerializationFwd.h"
25#include "llvm/Support/Allocator.h"
26#include <vector>
27
28namespace llvm {
29  struct fltSemantics;
30}
31
32namespace clang {
33  class ASTRecordLayout;
34  class Expr;
35  class IdentifierTable;
36  class SelectorTable;
37  class SourceManager;
38  class TargetInfo;
39  // Decls
40  class Decl;
41  class ObjCPropertyDecl;
42  class RecordDecl;
43  class TagDecl;
44  class TranslationUnitDecl;
45  class TypeDecl;
46  class TypedefDecl;
47
48/// ASTContext - This class holds long-lived AST nodes (such as types and
49/// decls) that can be referred to throughout the semantic analysis of a file.
50class ASTContext {
51  std::vector<Type*> Types;
52  llvm::FoldingSet<ASQualType> ASQualTypes;
53  llvm::FoldingSet<ComplexType> ComplexTypes;
54  llvm::FoldingSet<PointerType> PointerTypes;
55  llvm::FoldingSet<BlockPointerType> BlockPointerTypes;
56  llvm::FoldingSet<ReferenceType> ReferenceTypes;
57  llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes;
58  llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes;
59  std::vector<VariableArrayType*> VariableArrayTypes;
60  llvm::FoldingSet<VectorType> VectorTypes;
61  llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos;
62  llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos;
63  llvm::FoldingSet<ObjCQualifiedInterfaceType> ObjCQualifiedInterfaceTypes;
64  llvm::FoldingSet<ObjCQualifiedIdType> ObjCQualifiedIdTypes;
65  /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts.
66  ///  This is lazily created.  This is intentionally not serialized.
67  llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts;
68  llvm::DenseMap<const ObjCInterfaceDecl*,
69                 const ASTRecordLayout*> ASTObjCInterfaces;
70
71  /// BuiltinVaListType - built-in va list type.
72  /// This is initially null and set by Sema::LazilyCreateBuiltin when
73  /// a builtin that takes a valist is encountered.
74  QualType BuiltinVaListType;
75
76  /// ObjCIdType - a pseudo built-in typedef type (set by Sema).
77  QualType ObjCIdType;
78  const RecordType *IdStructType;
79
80  /// ObjCSelType - another pseudo built-in typedef type (set by Sema).
81  QualType ObjCSelType;
82  const RecordType *SelStructType;
83
84  /// ObjCProtoType - another pseudo built-in typedef type (set by Sema).
85  QualType ObjCProtoType;
86  const RecordType *ProtoStructType;
87
88  /// ObjCClassType - another pseudo built-in typedef type (set by Sema).
89  QualType ObjCClassType;
90  const RecordType *ClassStructType;
91
92  QualType ObjCConstantStringType;
93  RecordDecl *CFConstantStringTypeDecl;
94
95  RecordDecl *ObjCFastEnumerationStateTypeDecl;
96
97  TranslationUnitDecl *TUDecl;
98
99  /// SourceMgr - The associated SourceManager object.
100  SourceManager &SourceMgr;
101
102  /// LangOpts - The language options used to create the AST associated with
103  ///  this ASTContext object.
104  LangOptions LangOpts;
105
106  /// Allocator - The allocator object used to create AST objects.
107  llvm::MallocAllocator Allocator;
108
109public:
110  TargetInfo &Target;
111  IdentifierTable &Idents;
112  SelectorTable &Selectors;
113
114  SourceManager& getSourceManager() { return SourceMgr; }
115  llvm::MallocAllocator &getAllocator() { return Allocator; }
116  const LangOptions& getLangOptions() const { return LangOpts; }
117
118  FullSourceLoc getFullLoc(SourceLocation Loc) const {
119    return FullSourceLoc(Loc,SourceMgr);
120  }
121
122  TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; }
123
124  /// This is intentionally not serialized.  It is populated by the
125  /// ASTContext ctor, and there are no external pointers/references to
126  /// internal variables of BuiltinInfo.
127  Builtin::Context BuiltinInfo;
128
129  // Builtin Types.
130  QualType VoidTy;
131  QualType BoolTy;
132  QualType CharTy;
133  QualType WCharTy; // [C++ 3.9.1p5]
134  QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy;
135  QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
136  QualType UnsignedLongLongTy;
137  QualType FloatTy, DoubleTy, LongDoubleTy;
138  QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
139  QualType VoidPtrTy;
140  QualType OverloadTy;
141
142  ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t,
143             IdentifierTable &idents, SelectorTable &sels,
144             unsigned size_reserve=0);
145
146  ~ASTContext();
147
148  void PrintStats() const;
149  const std::vector<Type*>& getTypes() const { return Types; }
150
151  //===--------------------------------------------------------------------===//
152  //                           Type Constructors
153  //===--------------------------------------------------------------------===//
154
155  /// getASQualType - Return the uniqued reference to the type for an address
156  /// space qualified type with the specified type and address space.  The
157  /// resulting type has a union of the qualifiers from T and the address space.
158  // If T already has an address space specifier, it is silently replaced.
159  QualType getASQualType(QualType T, unsigned AddressSpace);
160
161  /// getComplexType - Return the uniqued reference to the type for a complex
162  /// number with the specified element type.
163  QualType getComplexType(QualType T);
164
165  /// getPointerType - Return the uniqued reference to the type for a pointer to
166  /// the specified type.
167  QualType getPointerType(QualType T);
168
169  /// getBlockPointerType - Return the uniqued reference to the type for a block
170  /// of the specified type.
171  QualType getBlockPointerType(QualType T);
172
173  /// getReferenceType - Return the uniqued reference to the type for a
174  /// reference to the specified type.
175  QualType getReferenceType(QualType T);
176
177  /// getVariableArrayType - Returns a non-unique reference to the type for a
178  /// variable array of the specified element type.
179  QualType getVariableArrayType(QualType EltTy, Expr *NumElts,
180                                ArrayType::ArraySizeModifier ASM,
181                                unsigned EltTypeQuals);
182
183  /// getIncompleteArrayType - Returns a unique reference to the type for a
184  /// incomplete array of the specified element type.
185  QualType getIncompleteArrayType(QualType EltTy,
186                                  ArrayType::ArraySizeModifier ASM,
187                                  unsigned EltTypeQuals);
188
189  /// getConstantArrayType - Return the unique reference to the type for a
190  /// constant array of the specified element type.
191  QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize,
192                                ArrayType::ArraySizeModifier ASM,
193                                unsigned EltTypeQuals);
194
195  /// getVectorType - Return the unique reference to a vector type of
196  /// the specified element type and size. VectorType must be a built-in type.
197  QualType getVectorType(QualType VectorType, unsigned NumElts);
198
199  /// getExtVectorType - Return the unique reference to an extended vector type
200  /// of the specified element type and size.  VectorType must be a built-in
201  /// type.
202  QualType getExtVectorType(QualType VectorType, unsigned NumElts);
203
204  /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
205  ///
206  QualType getFunctionTypeNoProto(QualType ResultTy);
207
208  /// getFunctionType - Return a normal function type with a typed argument
209  /// list.  isVariadic indicates whether the argument list includes '...'.
210  QualType getFunctionType(QualType ResultTy, const QualType *ArgArray,
211                           unsigned NumArgs, bool isVariadic,
212                           unsigned TypeQuals);
213
214  /// getTypeDeclType - Return the unique reference to the type for
215  /// the specified type declaration.
216  QualType getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl=0);
217
218  /// getTypedefType - Return the unique reference to the type for the
219  /// specified typename decl.
220  QualType getTypedefType(TypedefDecl *Decl);
221  QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl);
222
223  /// getObjCQualifiedInterfaceType - Return a
224  /// ObjCQualifiedInterfaceType type for the given interface decl and
225  /// the conforming protocol list.
226  QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
227             ObjCProtocolDecl **ProtocolList, unsigned NumProtocols);
228
229  /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a
230  /// given 'id' and conforming protocol list.
231  QualType getObjCQualifiedIdType(ObjCProtocolDecl **ProtocolList,
232                                  unsigned NumProtocols);
233
234
235  /// getTypeOfType - GCC extension.
236  QualType getTypeOfExpr(Expr *e);
237  QualType getTypeOfType(QualType t);
238
239  /// getTagDeclType - Return the unique reference to the type for the
240  /// specified TagDecl (struct/union/class/enum) decl.
241  QualType getTagDeclType(TagDecl *Decl);
242
243  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
244  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
245  QualType getSizeType() const;
246
247  /// getWCharType - Return the unique type for "wchar_t" (C99 7.17), defined
248  /// in <stddef.h>. Wide strings require this (C99 6.4.5p5).
249  QualType getWCharType() const;
250
251  /// getSignedWCharType - Return the type of "signed wchar_t".
252  /// Used when in C++, as a GCC extension.
253  QualType getSignedWCharType() const;
254
255  /// getUnsignedWCharType - Return the type of "unsigned wchar_t".
256  /// Used when in C++, as a GCC extension.
257  QualType getUnsignedWCharType() const;
258
259  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
260  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
261  QualType getPointerDiffType() const;
262
263  // getCFConstantStringType - Return the C structure type used to represent
264  // constant CFStrings.
265  QualType getCFConstantStringType();
266
267  // This setter/getter represents the ObjC type for an NSConstantString.
268  void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl);
269  QualType getObjCConstantStringInterface() const {
270    return ObjCConstantStringType;
271  }
272
273  //// This gets the struct used to keep track of fast enumerations.
274  QualType getObjCFastEnumerationStateType();
275
276  /// getObjCEncodingForType - Emit the ObjC type encoding for the
277  /// given type into \arg S. If \arg NameFields is specified then
278  /// record field names are also encoded.
279  void getObjCEncodingForType(QualType t, std::string &S,
280                              bool NameFields=false) const;
281
282  // Put the string version of type qualifiers into S.
283  void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
284                                       std::string &S) const;
285
286  /// getObjCEncodingForMethodDecl - Return the encoded type for this method
287  /// declaration.
288  void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S);
289
290  /// getObjCEncodingForPropertyDecl - Return the encoded type for
291  /// this method declaration. If non-NULL, Container must be either
292  /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should
293  /// only be NULL when getting encodings for protocol properties.
294  void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
295                                      const Decl *Container,
296                                      std::string &S);
297
298  /// getObjCEncodingTypeSize returns size of type for objective-c encoding
299  /// purpose.
300  int getObjCEncodingTypeSize(QualType t);
301
302  /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by
303  /// Sema.  id is always a (typedef for a) pointer type, a pointer to a struct.
304  QualType getObjCIdType() const { return ObjCIdType; }
305  void setObjCIdType(TypedefDecl *Decl);
306
307  void setObjCSelType(TypedefDecl *Decl);
308  QualType getObjCSelType() const { return ObjCSelType; }
309
310  void setObjCProtoType(QualType QT);
311  QualType getObjCProtoType() const { return ObjCProtoType; }
312
313  /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by
314  /// Sema.  'Class' is always a (typedef for a) pointer type, a pointer to a
315  /// struct.
316  QualType getObjCClassType() const { return ObjCClassType; }
317  void setObjCClassType(TypedefDecl *Decl);
318
319  void setBuiltinVaListType(QualType T);
320  QualType getBuiltinVaListType() const { return BuiltinVaListType; }
321
322private:
323  QualType getFromTargetType(unsigned Type) const;
324
325  //===--------------------------------------------------------------------===//
326  //                         Type Predicates.
327  //===--------------------------------------------------------------------===//
328
329public:
330  /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
331  /// to an object type.  This includes "id" and "Class" (two 'special' pointers
332  /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
333  /// ID type).
334  bool isObjCObjectPointerType(QualType Ty) const;
335
336  //===--------------------------------------------------------------------===//
337  //                         Type Sizing and Analysis
338  //===--------------------------------------------------------------------===//
339
340  /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
341  /// scalar floating point type.
342  const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const;
343
344  /// getTypeInfo - Get the size and alignment of the specified complete type in
345  /// bits.
346  std::pair<uint64_t, unsigned> getTypeInfo(const Type *T);
347  std::pair<uint64_t, unsigned> getTypeInfo(QualType T) {
348    return getTypeInfo(T.getTypePtr());
349  }
350
351  /// getTypeSize - Return the size of the specified type, in bits.  This method
352  /// does not work on incomplete types.
353  uint64_t getTypeSize(QualType T) {
354    return getTypeInfo(T).first;
355  }
356  uint64_t getTypeSize(const Type *T) {
357    return getTypeInfo(T).first;
358  }
359
360  /// getTypeAlign - Return the alignment of the specified type, in bits.  This
361  /// method does not work on incomplete types.
362  unsigned getTypeAlign(QualType T) {
363    return getTypeInfo(T).second;
364  }
365  unsigned getTypeAlign(const Type *T) {
366    return getTypeInfo(T).second;
367  }
368
369  /// getASTRecordLayout - Get or compute information about the layout of the
370  /// specified record (struct/union/class), which indicates its size and field
371  /// position information.
372  const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D);
373
374  const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D);
375  //===--------------------------------------------------------------------===//
376  //                            Type Operators
377  //===--------------------------------------------------------------------===//
378
379  /// getCanonicalType - Return the canonical (structural) type corresponding to
380  /// the specified potentially non-canonical type.  The non-canonical version
381  /// of a type may have many "decorated" versions of types.  Decorators can
382  /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
383  /// to be free of any of these, allowing two canonical types to be compared
384  /// for exact equality with a simple pointer comparison.
385  QualType getCanonicalType(QualType T);
386  const Type *getCanonicalType(const Type *T) {
387    return T->getCanonicalTypeInternal().getTypePtr();
388  }
389
390  /// Type Query functions.  If the type is an instance of the specified class,
391  /// return the Type pointer for the underlying maximally pretty type.  This
392  /// is a member of ASTContext because this may need to do some amount of
393  /// canonicalization, e.g. to move type qualifiers into the element type.
394  const ArrayType *getAsArrayType(QualType T);
395  const ConstantArrayType *getAsConstantArrayType(QualType T) {
396    return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T));
397  }
398  const VariableArrayType *getAsVariableArrayType(QualType T) {
399    return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T));
400  }
401  const IncompleteArrayType *getAsIncompleteArrayType(QualType T) {
402    return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T));
403  }
404
405  /// getArrayDecayedType - Return the properly qualified result of decaying the
406  /// specified array type to a pointer.  This operation is non-trivial when
407  /// handling typedefs etc.  The canonical type of "T" must be an array type,
408  /// this returns a pointer to a properly qualified element of the array.
409  ///
410  /// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
411  QualType getArrayDecayedType(QualType T);
412
413  /// getIntegerTypeOrder - Returns the highest ranked integer type:
414  /// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
415  /// LHS < RHS, return -1.
416  int getIntegerTypeOrder(QualType LHS, QualType RHS);
417
418  /// getFloatingTypeOrder - Compare the rank of the two specified floating
419  /// point types, ignoring the domain of the type (i.e. 'double' ==
420  /// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
421  /// LHS < RHS, return -1.
422  int getFloatingTypeOrder(QualType LHS, QualType RHS);
423
424  /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
425  /// point or a complex type (based on typeDomain/typeSize).
426  /// 'typeDomain' is a real floating point or complex type.
427  /// 'typeSize' is a real floating point or complex type.
428  QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
429                                             QualType typeDomain) const;
430
431  //===--------------------------------------------------------------------===//
432  //                    Type Compatibility Predicates
433  //===--------------------------------------------------------------------===//
434
435  /// Compatibility predicates used to check assignment expressions.
436  bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1
437  bool typesAreBlockCompatible(QualType lhs, QualType rhs);
438
439  bool isObjCIdType(QualType T) const {
440    if (!IdStructType) // ObjC isn't enabled
441      return false;
442    return T->getAsStructureType() == IdStructType;
443  }
444  bool isObjCClassType(QualType T) const {
445    if (!ClassStructType) // ObjC isn't enabled
446      return false;
447    return T->getAsStructureType() == ClassStructType;
448  }
449  bool isObjCSelType(QualType T) const {
450    assert(SelStructType && "isObjCSelType used before 'SEL' type is built");
451    return T->getAsStructureType() == SelStructType;
452  }
453
454  // Check the safety of assignment from LHS to RHS
455  bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
456                               const ObjCInterfaceType *RHS);
457
458  // Functions for calculating composite types
459  QualType mergeTypes(QualType, QualType);
460  QualType mergeFunctionTypes(QualType, QualType);
461
462  //===--------------------------------------------------------------------===//
463  //                    Integer Predicates
464  //===--------------------------------------------------------------------===//
465
466  // The width of an integer, as defined in C99 6.2.6.2. This is the number
467  // of bits in an integer type excluding any padding bits.
468  unsigned getIntWidth(QualType T);
469
470  // Per C99 6.2.5p6, for every signed integer type, there is a corresponding
471  // unsigned integer type.  This method takes a signed type, and returns the
472  // corresponding unsigned integer type.
473  QualType getCorrespondingUnsignedType(QualType T);
474
475  //===--------------------------------------------------------------------===//
476  //                    Type Iterators.
477  //===--------------------------------------------------------------------===//
478
479  typedef std::vector<Type*>::iterator       type_iterator;
480  typedef std::vector<Type*>::const_iterator const_type_iterator;
481
482  type_iterator types_begin() { return Types.begin(); }
483  type_iterator types_end() { return Types.end(); }
484  const_type_iterator types_begin() const { return Types.begin(); }
485  const_type_iterator types_end() const { return Types.end(); }
486
487  //===--------------------------------------------------------------------===//
488  //                    Serialization
489  //===--------------------------------------------------------------------===//
490
491  void Emit(llvm::Serializer& S) const;
492  static ASTContext* Create(llvm::Deserializer& D);
493
494private:
495  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
496  void operator=(const ASTContext&); // DO NOT IMPLEMENT
497
498  void InitBuiltinTypes();
499  void InitBuiltinType(QualType &R, BuiltinType::Kind K);
500
501  /// setRecordDefinition - Used by RecordDecl::defineBody to inform ASTContext
502  ///  about which RecordDecl serves as the definition of a particular
503  ///  struct/union/class.  This will eventually be used by enums as well.
504  void setTagDefinition(TagDecl* R);
505  friend class RecordDecl;
506
507  // Return the ObjC type encoding for a given type.
508  void getObjCEncodingForTypeImpl(QualType t, std::string &S,
509                                  bool ExpandPointedToStructures,
510                                  bool ExpandStructures,
511                                  bool NameFields) const;
512
513};
514
515}  // end namespace clang
516
517#endif
518