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