ASTContext.h revision 983df5b2280980e59b0b062bcc2882230465a61e
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 <vector>
25
26namespace clang {
27  class TargetInfo;
28  class IdentifierTable;
29
30/// ASTContext - This class holds long-lived AST nodes (such as types and
31/// decls) that can be referred to throughout the semantic analysis of a file.
32class ASTContext {
33  std::vector<Type*> Types;
34  llvm::FoldingSet<ComplexType> ComplexTypes;
35  llvm::FoldingSet<PointerType> PointerTypes;
36  llvm::FoldingSet<ReferenceType> ReferenceTypes;
37  llvm::FoldingSet<ConstantArrayType> ArrayTypes;
38  llvm::FoldingSet<VectorType> VectorTypes;
39  llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos;
40  llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos;
41  llvm::FoldingSet<ObjcQualifiedInterfaceType> ObjcQualifiedInterfaceTypes;
42  llvm::DenseMap<const RecordDecl*, const RecordLayout*> RecordLayoutInfo;
43
44  /// BuiltinVaListType - built-in va list type.
45  /// This is initially null and set by Sema::LazilyCreateBuiltin when
46  /// a builtin that takes a valist is encountered.
47  QualType BuiltinVaListType;
48
49  /// ObjcIdType - a psuedo built-in typedef type (set by Sema).
50  QualType ObjcIdType;
51  const RecordType *IdStructType;
52
53  QualType ObjcConstantStringType;
54  RecordDecl *CFConstantStringTypeDecl;
55public:
56
57  SourceManager &SourceMgr;
58  TargetInfo &Target;
59  IdentifierTable &Idents;
60  SelectorTable &Selectors;
61  Builtin::Context BuiltinInfo;
62
63  // Builtin Types.
64  QualType VoidTy;
65  QualType BoolTy;
66  QualType CharTy;
67  QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy;
68  QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
69  QualType UnsignedLongLongTy;
70  QualType FloatTy, DoubleTy, LongDoubleTy;
71  QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
72
73  ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents,
74             SelectorTable &sels) :
75    CFConstantStringTypeDecl(0), SourceMgr(SM), Target(t),
76    Idents(idents), Selectors(sels) {
77    InitBuiltinTypes();
78    BuiltinInfo.InitializeBuiltins(idents, Target);
79  }
80  ~ASTContext();
81
82  void PrintStats() const;
83
84  //===--------------------------------------------------------------------===//
85  //                           Type Constructors
86  //===--------------------------------------------------------------------===//
87
88  /// getComplexType - Return the uniqued reference to the type for a complex
89  /// number with the specified element type.
90  QualType getComplexType(QualType T);
91
92  /// getPointerType - Return the uniqued reference to the type for a pointer to
93  /// the specified type.
94  QualType getPointerType(QualType T);
95
96  /// getReferenceType - Return the uniqued reference to the type for a
97  /// reference to the specified type.
98  QualType getReferenceType(QualType T);
99
100  /// getVariableArrayType - Returns a non-unique reference to the type for a
101  /// variable array of the specified element type.
102  QualType getVariableArrayType(QualType EltTy, Expr *NumElts,
103                                ArrayType::ArraySizeModifier ASM,
104                                unsigned EltTypeQuals);
105
106  /// getConstantArrayType - Return the unique reference to the type for a
107  /// constant array of the specified element type.
108  QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize,
109                                ArrayType::ArraySizeModifier ASM,
110                                unsigned EltTypeQuals);
111
112  /// getVectorType - Return the unique reference to a vector type of
113  /// the specified element type and size. VectorType must be a built-in type.
114  QualType getVectorType(QualType VectorType, unsigned NumElts);
115
116  /// getOCUVectorType - Return the unique reference to an OCU vector type of
117  /// the specified element type and size. VectorType must be a built-in type.
118  QualType getOCUVectorType(QualType VectorType, unsigned NumElts);
119
120  /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
121  ///
122  QualType getFunctionTypeNoProto(QualType ResultTy);
123
124  /// getFunctionType - Return a normal function type with a typed argument
125  /// list.  isVariadic indicates whether the argument list includes '...'.
126  QualType getFunctionType(QualType ResultTy, QualType *ArgArray,
127                           unsigned NumArgs, bool isVariadic);
128
129  /// getTypedefType - Return the unique reference to the type for the
130  /// specified typename decl.
131  QualType getTypedefType(TypedefDecl *Decl);
132  QualType getObjcInterfaceType(ObjcInterfaceDecl *Decl);
133
134  /// getObjcQualifiedInterfaceType - Return a
135  /// ObjcQualifiedInterfaceType type for the given interface decl and
136  /// the conforming protocol list.
137  QualType getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
138             ObjcProtocolDecl **ProtocolList, unsigned NumProtocols);
139
140  /// getTypeOfType - GCC extension.
141  QualType getTypeOfExpr(Expr *e);
142  QualType getTypeOfType(QualType t);
143
144  /// getTagDeclType - Return the unique reference to the type for the
145  /// specified TagDecl (struct/union/class/enum) decl.
146  QualType getTagDeclType(TagDecl *Decl);
147
148  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
149  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
150  QualType getSizeType() const;
151
152  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
153  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
154  QualType getPointerDiffType() const;
155
156  // getCFConstantStringType - Return the type used for constant CFStrings.
157  // CURRENTLY UNUSED (10/15/07). ObjCStringLiteral now uses the hook below.
158  QualType getCFConstantStringType();
159
160  // This setter/getter represents the ObjC type for an NSConstantString.
161  void setObjcConstantStringInterface(ObjcInterfaceDecl *Decl);
162  QualType getObjcConstantStringInterface() const {
163    return ObjcConstantStringType;
164  }
165
166  // This setter/getter repreents the ObjC 'id' type. It is setup lazily, by
167  // Sema.
168  void setObjcIdType(TypedefDecl *Decl);
169  QualType getObjcIdType() const { return ObjcIdType; }
170
171  void setBuiltinVaListType(QualType T);
172  QualType getBuiltinVaListType() const { return BuiltinVaListType; }
173
174  //===--------------------------------------------------------------------===//
175  //                         Type Sizing and Analysis
176  //===--------------------------------------------------------------------===//
177
178  /// getTypeInfo - Get the size and alignment of the specified complete type in
179  /// bits.
180  std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L);
181
182  /// getTypeSize - Return the size of the specified type, in bits.  This method
183  /// does not work on incomplete types.
184  uint64_t getTypeSize(QualType T, SourceLocation L) {
185    return getTypeInfo(T, L).first;
186  }
187
188  /// getTypeAlign - Return the alignment of the specified type, in bits.  This
189  /// method does not work on incomplete types.
190  unsigned getTypeAlign(QualType T, SourceLocation L) {
191    return getTypeInfo(T, L).second;
192  }
193
194  /// getRecordLayout - Get or compute information about the layout of the
195  /// specified record (struct/union/class), which indicates its size and field
196  /// position information.
197  const RecordLayout &getRecordLayout(const RecordDecl *D, SourceLocation L);
198
199  //===--------------------------------------------------------------------===//
200  //                            Type Operators
201  //===--------------------------------------------------------------------===//
202
203  /// maxIntegerType - Returns the highest ranked integer type. Handles 3
204  /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned.
205  static QualType maxIntegerType(QualType lhs, QualType rhs);
206
207  /// compareFloatingType - Handles 3 different combos:
208  /// float/float, float/complex, complex/complex.
209  /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
210  static int compareFloatingType(QualType lt, QualType rt);
211
212  /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
213  /// point or a complex type (based on typeDomain/typeSize).
214  /// 'typeDomain' is a real floating point or complex type.
215  /// 'typeSize' is a real floating point or complex type.
216  QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
217                                             QualType typeDomain) const;
218
219  //===--------------------------------------------------------------------===//
220  //                    Type Compatibility Predicates
221  //===--------------------------------------------------------------------===//
222
223  /// Compatibility predicates used to check assignment expressions.
224  bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1
225  bool tagTypesAreCompatible(QualType, QualType); // C99 6.2.7p1
226  bool pointerTypesAreCompatible(QualType, QualType);  // C99 6.7.5.1p2
227  bool referenceTypesAreCompatible(QualType, QualType); // C++ 5.17p6
228  bool functionTypesAreCompatible(QualType, QualType); // C99 6.7.5.3p15
229  bool arrayTypesAreCompatible(QualType, QualType); // C99 6.7.5.2p6
230  bool builtinTypesAreCompatible(QualType, QualType);
231
232  /// Objective-C specific type checking.
233  bool interfaceTypesAreCompatible(QualType, QualType);
234  bool objcTypesAreCompatible(QualType, QualType);
235  bool isObjcIdType(QualType T) const {
236    return T->getAsStructureType() == IdStructType;
237  }
238private:
239  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
240  void operator=(const ASTContext&); // DO NOT IMPLEMENT
241
242  void InitBuiltinTypes();
243  void InitBuiltinType(QualType &R, BuiltinType::Kind K);
244};
245
246}  // end namespace clang
247
248#endif
249