1 //===--- ASTContext.h - Context to hold long-lived AST nodes ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ASTContext interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_ASTCONTEXT_H
15 #define LLVM_CLANG_AST_ASTCONTEXT_H
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"
29 class IdentifierTable;
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.
34 std::vector<Type*> Types;
35 llvm::FoldingSet<ASQualType> ASQualTypes;
36 llvm::FoldingSet<ComplexType> ComplexTypes;
37 llvm::FoldingSet<PointerType> PointerTypes;
38 llvm::FoldingSet<ReferenceType> ReferenceTypes;
39 llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes;
40 llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes;
41 std::vector<VariableArrayType*> VariableArrayTypes;
42 llvm::FoldingSet<VectorType> VectorTypes;
43 llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos;
44 llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos;
45 llvm::FoldingSet<ObjCQualifiedInterfaceType> ObjCQualifiedInterfaceTypes;
46 llvm::FoldingSet<ObjCQualifiedIdType> ObjCQualifiedIdTypes;
47 /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts.
48 /// This is lazily created. This is intentionally not serialized.
49 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts;
51 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
53 /// BuiltinVaListType - built-in va list type.
54 /// This is initially null and set by Sema::LazilyCreateBuiltin when
55 /// a builtin that takes a valist is encountered.
56 QualType BuiltinVaListType;
58 /// ObjCIdType - a pseudo built-in typedef type (set by Sema).
60 const RecordType *IdStructType;
62 /// ObjCSelType - another pseudo built-in typedef type (set by Sema).
64 const RecordType *SelStructType;
66 /// ObjCProtoType - another pseudo built-in typedef type (set by Sema).
67 QualType ObjCProtoType;
68 const RecordType *ProtoStructType;
70 /// ObjCClassType - another pseudo built-in typedef type (set by Sema).
71 QualType ObjCClassType;
72 const RecordType *ClassStructType;
74 QualType ObjCConstantStringType;
75 RecordDecl *CFConstantStringTypeDecl;
77 SourceManager &SourceMgr;
80 IdentifierTable &Idents;
81 SelectorTable &Selectors;
83 SourceManager& getSourceManager() { return SourceMgr; }
85 FullSourceLoc getFullLoc(SourceLocation Loc) const {
86 return FullSourceLoc(Loc,SourceMgr);
89 /// This is intentionally not serialized. It is populated by the
90 /// ASTContext ctor, and there are no external pointers/references to
91 /// internal variables of BuiltinInfo.
92 Builtin::Context BuiltinInfo;
98 QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy;
99 QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
100 QualType UnsignedLongLongTy;
101 QualType FloatTy, DoubleTy, LongDoubleTy;
102 QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
105 ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents,
106 SelectorTable &sels, unsigned size_reserve=0 ) :
107 CFConstantStringTypeDecl(0), SourceMgr(SM), Target(t),
108 Idents(idents), Selectors(sels) {
110 if (size_reserve > 0) Types.reserve(size_reserve);
112 BuiltinInfo.InitializeBuiltins(idents, Target);
117 void PrintStats() const;
118 const std::vector<Type*>& getTypes() const { return Types; }
120 //===--------------------------------------------------------------------===//
122 //===--------------------------------------------------------------------===//
124 /// getASQualType - Return the uniqued reference to the type for an address
125 /// space qualified type with the specified type and address space. The
126 /// resulting type has a union of the qualifiers from T and the address space.
127 // If T already has an address space specifier, it is silently replaced.
128 QualType getASQualType(QualType T, unsigned AddressSpace);
130 /// getComplexType - Return the uniqued reference to the type for a complex
131 /// number with the specified element type.
132 QualType getComplexType(QualType T);
134 /// getPointerType - Return the uniqued reference to the type for a pointer to
135 /// the specified type.
136 QualType getPointerType(QualType T);
138 /// getReferenceType - Return the uniqued reference to the type for a
139 /// reference to the specified type.
140 QualType getReferenceType(QualType T);
142 /// getVariableArrayType - Returns a non-unique reference to the type for a
143 /// variable array of the specified element type.
144 QualType getVariableArrayType(QualType EltTy, Expr *NumElts,
145 ArrayType::ArraySizeModifier ASM,
146 unsigned EltTypeQuals);
148 /// getIncompleteArrayType - Returns a unique reference to the type for a
149 /// incomplete array of the specified element type.
150 QualType getIncompleteArrayType(QualType EltTy,
151 ArrayType::ArraySizeModifier ASM,
152 unsigned EltTypeQuals);
154 /// getConstantArrayType - Return the unique reference to the type for a
155 /// constant array of the specified element type.
156 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize,
157 ArrayType::ArraySizeModifier ASM,
158 unsigned EltTypeQuals);
160 /// getVectorType - Return the unique reference to a vector type of
161 /// the specified element type and size. VectorType must be a built-in type.
162 QualType getVectorType(QualType VectorType, unsigned NumElts);
164 /// getOCUVectorType - Return the unique reference to an OCU vector type of
165 /// the specified element type and size. VectorType must be a built-in type.
166 QualType getOCUVectorType(QualType VectorType, unsigned NumElts);
168 /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
170 QualType getFunctionTypeNoProto(QualType ResultTy);
172 /// getFunctionType - Return a normal function type with a typed argument
173 /// list. isVariadic indicates whether the argument list includes '...'.
174 QualType getFunctionType(QualType ResultTy, QualType *ArgArray,
175 unsigned NumArgs, bool isVariadic);
177 /// getTypedefType - Return the unique reference to the type for the
178 /// specified typename decl.
179 QualType getTypedefType(TypedefDecl *Decl);
180 QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl);
182 /// getObjCQualifiedInterfaceType - Return a
183 /// ObjCQualifiedInterfaceType type for the given interface decl and
184 /// the conforming protocol list.
185 QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
186 ObjCProtocolDecl **ProtocolList, unsigned NumProtocols);
188 /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a
189 /// given 'id' and conforming protocol list.
190 QualType getObjCQualifiedIdType(QualType idType,
191 ObjCProtocolDecl **ProtocolList,
192 unsigned NumProtocols);
195 /// getTypeOfType - GCC extension.
196 QualType getTypeOfExpr(Expr *e);
197 QualType getTypeOfType(QualType t);
199 /// getTagDeclType - Return the unique reference to the type for the
200 /// specified TagDecl (struct/union/class/enum) decl.
201 QualType getTagDeclType(TagDecl *Decl);
203 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
204 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
205 QualType getSizeType() const;
207 /// getWcharType - Return the unique type for "wchar_t" (C99 7.17), defined
208 /// in <stddef.h>. Wide strings require this (C99 6.4.5p5).
209 QualType getWcharType() const;
211 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
212 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
213 QualType getPointerDiffType() const;
215 // getCFConstantStringType - Return the C structure type used to represent
216 // constant CFStrings.
217 QualType getCFConstantStringType();
219 // This setter/getter represents the ObjC type for an NSConstantString.
220 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl);
221 QualType getObjCConstantStringInterface() const {
222 return ObjCConstantStringType;
225 // Return the ObjC type encoding for a given type.
226 void getObjCEncodingForType(QualType t, std::string &S,
227 llvm::SmallVector<const RecordType *, 8> &RT) const;
229 // Put the string version of type qualifiers into S.
230 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
231 std::string &S) const;
233 /// getObjCEncodingForMethodDecl - Return the encoded type for this method
235 void getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl, std::string &S);
237 /// getObjCEncodingTypeSize returns size of type for objective-c encoding
239 int getObjCEncodingTypeSize(QualType t);
241 // This setter/getter repreents the ObjC 'id' type. It is setup lazily, by
243 void setObjCIdType(TypedefDecl *Decl);
244 QualType getObjCIdType() const { return ObjCIdType; }
246 void setObjCSelType(TypedefDecl *Decl);
247 QualType getObjCSelType() const { return ObjCSelType; }
249 void setObjCProtoType(QualType QT);
250 QualType getObjCProtoType() const { return ObjCProtoType; }
252 void setObjCClassType(TypedefDecl *Decl);
253 QualType getObjCClassType() const { return ObjCClassType; }
255 void setBuiltinVaListType(QualType T);
256 QualType getBuiltinVaListType() const { return BuiltinVaListType; }
258 //===--------------------------------------------------------------------===//
259 // Type Sizing and Analysis
260 //===--------------------------------------------------------------------===//
262 /// getTypeInfo - Get the size and alignment of the specified complete type in
264 std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L);
266 /// getTypeSize - Return the size of the specified type, in bits. This method
267 /// does not work on incomplete types.
268 uint64_t getTypeSize(QualType T, SourceLocation L) {
269 return getTypeInfo(T, L).first;
272 /// getTypeAlign - Return the alignment of the specified type, in bits. This
273 /// method does not work on incomplete types.
274 unsigned getTypeAlign(QualType T, SourceLocation L) {
275 return getTypeInfo(T, L).second;
278 /// getASTRecordLayout - Get or compute information about the layout of the
279 /// specified record (struct/union/class), which indicates its size and field
280 /// position information.
281 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D, SourceLocation L);
283 //===--------------------------------------------------------------------===//
285 //===--------------------------------------------------------------------===//
287 /// maxIntegerType - Returns the highest ranked integer type. Handles 3
288 /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned.
289 static QualType maxIntegerType(QualType lhs, QualType rhs);
291 /// compareFloatingType - Handles 3 different combos:
292 /// float/float, float/complex, complex/complex.
293 /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
294 static int compareFloatingType(QualType lt, QualType rt);
296 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
297 /// point or a complex type (based on typeDomain/typeSize).
298 /// 'typeDomain' is a real floating point or complex type.
299 /// 'typeSize' is a real floating point or complex type.
300 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
301 QualType typeDomain) const;
303 //===--------------------------------------------------------------------===//
304 // Type Compatibility Predicates
305 //===--------------------------------------------------------------------===//
307 /// Compatibility predicates used to check assignment expressions.
308 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1
309 bool tagTypesAreCompatible(QualType, QualType); // C99 6.2.7p1
310 bool pointerTypesAreCompatible(QualType, QualType); // C99 6.7.5.1p2
311 bool referenceTypesAreCompatible(QualType, QualType); // C++ 5.17p6
312 bool functionTypesAreCompatible(QualType, QualType); // C99 6.7.5.3p15
313 bool arrayTypesAreCompatible(QualType, QualType); // C99 6.7.5.2p6
314 bool builtinTypesAreCompatible(QualType, QualType);
315 bool vectorTypesAreCompatible(QualType, QualType);
317 bool QualifiedInterfaceTypesAreCompatible(QualType, QualType);
318 bool ObjCQualifiedIdTypesAreCompatible(QualType, QualType, bool = false);
319 bool objcTypesAreCompatible(QualType, QualType);
320 bool isObjCIdType(QualType T) const {
321 if (!IdStructType) // ObjC isn't enabled
323 return T->getAsStructureType() == IdStructType;
325 bool isObjCClassType(QualType T) const {
326 if (!ClassStructType) // ObjC isn't enabled
328 return T->getAsStructureType() == ClassStructType;
330 bool isObjCSelType(QualType T) const {
331 assert(SelStructType && "isObjCSelType used before 'SEL' type is built");
332 return T->getAsStructureType() == SelStructType;
336 ASTContext(const ASTContext&); // DO NOT IMPLEMENT
337 void operator=(const ASTContext&); // DO NOT IMPLEMENT
339 void InitBuiltinTypes();
340 void InitBuiltinType(QualType &R, BuiltinType::Kind K);
342 /// helper function for Objective-C specific type checking.
343 bool interfaceTypesAreCompatible(QualType, QualType);
345 //===--------------------------------------------------------------------===//
347 //===--------------------------------------------------------------------===//
350 void Emit(llvm::Serializer& S) const;
351 static ASTContext* Create(llvm::Deserializer& D);
354 } // end namespace clang