]> granicus.if.org Git - clang/blob - include/clang/AST/ASTContext.h
Alternate address spaces work:
[clang] / include / clang / AST / ASTContext.h
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/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"
25 #include <vector>
26
27 namespace clang {
28   class TargetInfo;
29   class IdentifierTable;
30   
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.
33 class ASTContext {  
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;
50   
51   llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
52     
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;
57   
58   /// ObjCIdType - a pseudo built-in typedef type (set by Sema).
59   QualType ObjCIdType;
60   const RecordType *IdStructType;
61   
62   /// ObjCSelType - another pseudo built-in typedef type (set by Sema).
63   QualType ObjCSelType;
64   const RecordType *SelStructType;
65   
66   /// ObjCProtoType - another pseudo built-in typedef type (set by Sema).
67   QualType ObjCProtoType;
68   const RecordType *ProtoStructType;
69
70   /// ObjCClassType - another pseudo built-in typedef type (set by Sema).
71   QualType ObjCClassType;
72   const RecordType *ClassStructType;
73   
74   QualType ObjCConstantStringType;
75   RecordDecl *CFConstantStringTypeDecl;
76
77   SourceManager &SourceMgr;
78 public:
79   TargetInfo &Target;
80   IdentifierTable &Idents;
81   SelectorTable &Selectors;
82   
83   SourceManager& getSourceManager() { return SourceMgr; }
84
85   FullSourceLoc getFullLoc(SourceLocation Loc) const { 
86     return FullSourceLoc(Loc,SourceMgr);
87   }
88   
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;
93
94   // Builtin Types.
95   QualType VoidTy;
96   QualType BoolTy;
97   QualType CharTy;
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;
103   QualType VoidPtrTy;
104   
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) {
109
110     if (size_reserve > 0) Types.reserve(size_reserve);    
111     InitBuiltinTypes();
112     BuiltinInfo.InitializeBuiltins(idents, Target);
113   }
114
115   ~ASTContext();
116   
117   void PrintStats() const;
118   const std::vector<Type*>& getTypes() const { return Types; }
119   
120   //===--------------------------------------------------------------------===//
121   //                           Type Constructors
122   //===--------------------------------------------------------------------===//
123   
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);
129   
130   /// getComplexType - Return the uniqued reference to the type for a complex
131   /// number with the specified element type.
132   QualType getComplexType(QualType T);
133   
134   /// getPointerType - Return the uniqued reference to the type for a pointer to
135   /// the specified type.
136   QualType getPointerType(QualType T);
137   
138   /// getReferenceType - Return the uniqued reference to the type for a
139   /// reference to the specified type.
140   QualType getReferenceType(QualType T);
141   
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);
147
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);
153
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);
159                         
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);
163
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);
167
168   /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
169   ///
170   QualType getFunctionTypeNoProto(QualType ResultTy);
171   
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);
176   
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);
181   
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);
187   
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);
193                                   
194
195   /// getTypeOfType - GCC extension.
196   QualType getTypeOfExpr(Expr *e);
197   QualType getTypeOfType(QualType t);
198   
199   /// getTagDeclType - Return the unique reference to the type for the
200   /// specified TagDecl (struct/union/class/enum) decl.
201   QualType getTagDeclType(TagDecl *Decl);
202   
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;
206
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;
210   
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;
214   
215   // getCFConstantStringType - Return the C structure type used to represent
216   // constant CFStrings.
217   QualType getCFConstantStringType(); 
218   
219   // This setter/getter represents the ObjC type for an NSConstantString.
220   void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl);
221   QualType getObjCConstantStringInterface() const { 
222     return ObjCConstantStringType; 
223   }
224
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;
228   
229   // Put the string version of type qualifiers into S.
230   void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 
231                                        std::string &S) const;
232   
233   /// getObjCEncodingForMethodDecl - Return the encoded type for this method
234   /// declaration.
235   void getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl, std::string &S);
236   
237   /// getObjCEncodingTypeSize returns size of type for objective-c encoding
238   /// purpose.
239   int getObjCEncodingTypeSize(QualType t);
240     
241   // This setter/getter repreents the ObjC 'id' type. It is setup lazily, by
242   // Sema.
243   void setObjCIdType(TypedefDecl *Decl);
244   QualType getObjCIdType() const { return ObjCIdType; }
245   
246   void setObjCSelType(TypedefDecl *Decl);
247   QualType getObjCSelType() const { return ObjCSelType; }
248   
249   void setObjCProtoType(QualType QT);
250   QualType getObjCProtoType() const { return ObjCProtoType; }
251   
252   void setObjCClassType(TypedefDecl *Decl);
253   QualType getObjCClassType() const { return ObjCClassType; }
254   
255   void setBuiltinVaListType(QualType T);
256   QualType getBuiltinVaListType() const { return BuiltinVaListType; }
257     
258   //===--------------------------------------------------------------------===//
259   //                         Type Sizing and Analysis
260   //===--------------------------------------------------------------------===//
261   
262   /// getTypeInfo - Get the size and alignment of the specified complete type in
263   /// bits.
264   std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L);
265   
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;
270   }
271   
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;
276   }
277   
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);
282   
283   //===--------------------------------------------------------------------===//
284   //                            Type Operators
285   //===--------------------------------------------------------------------===//
286   
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);
290   
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);
295
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;
302
303   //===--------------------------------------------------------------------===//
304   //                    Type Compatibility Predicates
305   //===--------------------------------------------------------------------===//
306                                              
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);
316   
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
322       return false;
323     return T->getAsStructureType() == IdStructType;
324   }
325   bool isObjCClassType(QualType T) const {
326     if (!ClassStructType) // ObjC isn't enabled
327       return false;
328     return T->getAsStructureType() == ClassStructType;
329   }
330   bool isObjCSelType(QualType T) const {
331     assert(SelStructType && "isObjCSelType used before 'SEL' type is built");
332     return T->getAsStructureType() == SelStructType;
333   }
334
335 private:
336   ASTContext(const ASTContext&); // DO NOT IMPLEMENT
337   void operator=(const ASTContext&); // DO NOT IMPLEMENT
338   
339   void InitBuiltinTypes();
340   void InitBuiltinType(QualType &R, BuiltinType::Kind K);
341
342   /// helper function for Objective-C specific type checking.
343   bool interfaceTypesAreCompatible(QualType, QualType);
344   
345   //===--------------------------------------------------------------------===//
346   //                    Serialization
347   //===--------------------------------------------------------------------===//
348
349 public:
350   void Emit(llvm::Serializer& S) const;
351   static ASTContext* Create(llvm::Deserializer& D);  
352 };
353   
354 }  // end namespace clang
355
356 #endif