ASTContext.h revision 66c5dfc193c4090b67c5effb0e80e287b94de17e
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 "llvm/Bitcode/SerializationFwd.h" 25#include <vector> 26 27namespace 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. 33class ASTContext { 34 std::vector<Type*> Types; 35 llvm::FoldingSet<ComplexType> ComplexTypes; 36 llvm::FoldingSet<PointerType> PointerTypes; 37 llvm::FoldingSet<ReferenceType> ReferenceTypes; 38 llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes; 39 llvm::FoldingSet<VariableArrayType> IncompleteVariableArrayTypes; 40 std::vector<VariableArrayType*> CompleteVariableArrayTypes; 41 llvm::FoldingSet<VectorType> VectorTypes; 42 llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos; 43 llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos; 44 llvm::FoldingSet<ObjcQualifiedInterfaceType> ObjcQualifiedInterfaceTypes; 45 /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts. 46 /// This is lazily created. This is intentionally not serialized. 47 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts; 48 49 /// BuiltinVaListType - built-in va list type. 50 /// This is initially null and set by Sema::LazilyCreateBuiltin when 51 /// a builtin that takes a valist is encountered. 52 QualType BuiltinVaListType; 53 54 /// ObjcIdType - a psuedo built-in typedef type (set by Sema). 55 QualType ObjcIdType; 56 const RecordType *IdStructType; 57 58 /// ObjcSelType - another psuedo built-in typedef type (set by Sema). 59 QualType ObjcSelType; 60 const RecordType *SelStructType; 61 62 /// ObjcProtoType - another psuedo built-in typedef type (set by Sema). 63 QualType ObjcProtoType; 64 const RecordType *ProtoStructType; 65 66 /// ObjcClassType - another psuedo built-in typedef type (set by Sema). 67 QualType ObjcClassType; 68 const RecordType *ClassStructType; 69 70 QualType ObjcConstantStringType; 71 RecordDecl *CFConstantStringTypeDecl; 72public: 73 74 SourceManager &SourceMgr; 75 TargetInfo &Target; 76 IdentifierTable &Idents; 77 SelectorTable &Selectors; 78 79 /// This is intentionally not serialized. It is populated by the 80 /// ASTContext ctor, and there are no external pointers/references to 81 /// internal variables of BuiltinInfo. 82 Builtin::Context BuiltinInfo; 83 84 // Builtin Types. 85 QualType VoidTy; 86 QualType BoolTy; 87 QualType CharTy; 88 QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy; 89 QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy; 90 QualType UnsignedLongLongTy; 91 QualType FloatTy, DoubleTy, LongDoubleTy; 92 QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; 93 QualType VoidPtrTy; 94 95 ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents, 96 SelectorTable &sels, unsigned size_reserve=0 ) : 97 CFConstantStringTypeDecl(0), SourceMgr(SM), Target(t), 98 Idents(idents), Selectors(sels) { 99 100 if (size_reserve > 0) Types.reserve(size_reserve); 101 InitBuiltinTypes(); 102 BuiltinInfo.InitializeBuiltins(idents, Target); 103 } 104 105 ~ASTContext(); 106 107 void PrintStats() const; 108 const std::vector<Type*>& getTypes() const { return Types; } 109 110 //===--------------------------------------------------------------------===// 111 // Type Constructors 112 //===--------------------------------------------------------------------===// 113 114 /// getComplexType - Return the uniqued reference to the type for a complex 115 /// number with the specified element type. 116 QualType getComplexType(QualType T); 117 118 /// getPointerType - Return the uniqued reference to the type for a pointer to 119 /// the specified type. 120 QualType getPointerType(QualType T); 121 122 /// getReferenceType - Return the uniqued reference to the type for a 123 /// reference to the specified type. 124 QualType getReferenceType(QualType T); 125 126 /// getVariableArrayType - Returns a non-unique reference to the type for a 127 /// variable array of the specified element type. 128 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 129 ArrayType::ArraySizeModifier ASM, 130 unsigned EltTypeQuals); 131 132 /// getConstantArrayType - Return the unique reference to the type for a 133 /// constant array of the specified element type. 134 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 135 ArrayType::ArraySizeModifier ASM, 136 unsigned EltTypeQuals); 137 138 /// getVectorType - Return the unique reference to a vector type of 139 /// the specified element type and size. VectorType must be a built-in type. 140 QualType getVectorType(QualType VectorType, unsigned NumElts); 141 142 /// getOCUVectorType - Return the unique reference to an OCU vector type of 143 /// the specified element type and size. VectorType must be a built-in type. 144 QualType getOCUVectorType(QualType VectorType, unsigned NumElts); 145 146 /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. 147 /// 148 QualType getFunctionTypeNoProto(QualType ResultTy); 149 150 /// getFunctionType - Return a normal function type with a typed argument 151 /// list. isVariadic indicates whether the argument list includes '...'. 152 QualType getFunctionType(QualType ResultTy, QualType *ArgArray, 153 unsigned NumArgs, bool isVariadic); 154 155 /// getTypedefType - Return the unique reference to the type for the 156 /// specified typename decl. 157 QualType getTypedefType(TypedefDecl *Decl); 158 QualType getObjcInterfaceType(ObjcInterfaceDecl *Decl); 159 160 /// getObjcQualifiedInterfaceType - Return a 161 /// ObjcQualifiedInterfaceType type for the given interface decl and 162 /// the conforming protocol list. 163 QualType getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl, 164 ObjcProtocolDecl **ProtocolList, unsigned NumProtocols); 165 166 /// getTypeOfType - GCC extension. 167 QualType getTypeOfExpr(Expr *e); 168 QualType getTypeOfType(QualType t); 169 170 /// getTagDeclType - Return the unique reference to the type for the 171 /// specified TagDecl (struct/union/class/enum) decl. 172 QualType getTagDeclType(TagDecl *Decl); 173 174 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 175 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 176 QualType getSizeType() const; 177 178 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 179 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 180 QualType getPointerDiffType() const; 181 182 // getCFConstantStringType - Return the C structure type used to represent 183 // constant CFStrings. 184 QualType getCFConstantStringType(); 185 186 // This setter/getter represents the ObjC type for an NSConstantString. 187 void setObjcConstantStringInterface(ObjcInterfaceDecl *Decl); 188 QualType getObjcConstantStringInterface() const { 189 return ObjcConstantStringType; 190 } 191 192 // Return the ObjC type encoding for a given type. 193 void getObjcEncodingForType(QualType t, std::string &S) const; 194 195 // Put the string version of type qualifiers into S. 196 void getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT, 197 std::string &S) const; 198 199 /// getObjcEncodingForMethodDecl - Return the encoded type for this method 200 /// declaration. 201 void getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl, std::string &S); 202 203 /// getObjcEncodingTypeSize returns size of type for objective-c encoding 204 /// purpose. 205 int getObjcEncodingTypeSize(QualType t); 206 207 // This setter/getter repreents the ObjC 'id' type. It is setup lazily, by 208 // Sema. 209 void setObjcIdType(TypedefDecl *Decl); 210 QualType getObjcIdType() const { return ObjcIdType; } 211 212 void setObjcSelType(TypedefDecl *Decl); 213 QualType getObjcSelType() const { return ObjcSelType; } 214 215 void setObjcProtoType(QualType QT); 216 QualType getObjcProtoType() const { return ObjcProtoType; } 217 218 void setObjcClassType(TypedefDecl *Decl); 219 QualType getObjcClassType() const { return ObjcClassType; } 220 221 void setBuiltinVaListType(QualType T); 222 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 223 224 //===--------------------------------------------------------------------===// 225 // Type Sizing and Analysis 226 //===--------------------------------------------------------------------===// 227 228 /// getTypeInfo - Get the size and alignment of the specified complete type in 229 /// bits. 230 std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L); 231 232 /// getTypeSize - Return the size of the specified type, in bits. This method 233 /// does not work on incomplete types. 234 uint64_t getTypeSize(QualType T, SourceLocation L) { 235 return getTypeInfo(T, L).first; 236 } 237 238 /// getTypeAlign - Return the alignment of the specified type, in bits. This 239 /// method does not work on incomplete types. 240 unsigned getTypeAlign(QualType T, SourceLocation L) { 241 return getTypeInfo(T, L).second; 242 } 243 244 /// getASTRecordLayout - Get or compute information about the layout of the 245 /// specified record (struct/union/class), which indicates its size and field 246 /// position information. 247 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D, SourceLocation L); 248 249 //===--------------------------------------------------------------------===// 250 // Type Operators 251 //===--------------------------------------------------------------------===// 252 253 /// maxIntegerType - Returns the highest ranked integer type. Handles 3 254 /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned. 255 static QualType maxIntegerType(QualType lhs, QualType rhs); 256 257 /// compareFloatingType - Handles 3 different combos: 258 /// float/float, float/complex, complex/complex. 259 /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1. 260 static int compareFloatingType(QualType lt, QualType rt); 261 262 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 263 /// point or a complex type (based on typeDomain/typeSize). 264 /// 'typeDomain' is a real floating point or complex type. 265 /// 'typeSize' is a real floating point or complex type. 266 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 267 QualType typeDomain) const; 268 269 //===--------------------------------------------------------------------===// 270 // Type Compatibility Predicates 271 //===--------------------------------------------------------------------===// 272 273 /// Compatibility predicates used to check assignment expressions. 274 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 275 bool tagTypesAreCompatible(QualType, QualType); // C99 6.2.7p1 276 bool pointerTypesAreCompatible(QualType, QualType); // C99 6.7.5.1p2 277 bool referenceTypesAreCompatible(QualType, QualType); // C++ 5.17p6 278 bool functionTypesAreCompatible(QualType, QualType); // C99 6.7.5.3p15 279 bool arrayTypesAreCompatible(QualType, QualType); // C99 6.7.5.2p6 280 bool builtinTypesAreCompatible(QualType, QualType); 281 bool vectorTypesAreCompatible(QualType, QualType); 282 283 /// Objective-C specific type checking. 284 bool interfaceTypesAreCompatible(QualType, QualType); 285 bool objcTypesAreCompatible(QualType, QualType); 286 bool isObjcIdType(QualType T) const { 287 if (!IdStructType) // ObjC isn't enabled 288 return false; 289 return T->getAsStructureType() == IdStructType; 290 } 291 bool isObjcClassType(QualType T) const { 292 if (!ClassStructType) // ObjC isn't enabled 293 return false; 294 return T->getAsStructureType() == ClassStructType; 295 } 296 bool isObjcSelType(QualType T) const { 297 assert(SelStructType && "isObjcSelType used before 'SEL' type is built"); 298 return T->getAsStructureType() == SelStructType; 299 } 300 301private: 302 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 303 void operator=(const ASTContext&); // DO NOT IMPLEMENT 304 305 void InitBuiltinTypes(); 306 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 307 308 //===--------------------------------------------------------------------===// 309 // Serialization 310 //===--------------------------------------------------------------------===// 311 312public: 313 void Emit(llvm::Serializer& S) const; 314 static ASTContext* Create(llvm::Deserializer& D); 315}; 316 317} // end namespace clang 318 319#endif 320