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