ASTContext.h revision c5773c4b8ce1ed6ed5c7112c9020c954a47dce96
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 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<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; 78public: 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 /// getAddrSpaceQualType - Return the uniqued reference to the type for an 125 /// address space qualified type with the specified type and address space. 126 QualType getASQualType(QualType T, unsigned AddressSpace); 127 128 /// getComplexType - Return the uniqued reference to the type for a complex 129 /// number with the specified element type. 130 QualType getComplexType(QualType T); 131 132 /// getPointerType - Return the uniqued reference to the type for a pointer to 133 /// the specified type. 134 QualType getPointerType(QualType T); 135 136 /// getReferenceType - Return the uniqued reference to the type for a 137 /// reference to the specified type. 138 QualType getReferenceType(QualType T); 139 140 /// getVariableArrayType - Returns a non-unique reference to the type for a 141 /// variable array of the specified element type. 142 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 143 ArrayType::ArraySizeModifier ASM, 144 unsigned EltTypeQuals); 145 146 /// getIncompleteArrayType - Returns a unique reference to the type for a 147 /// incomplete array of the specified element type. 148 QualType getIncompleteArrayType(QualType EltTy, 149 ArrayType::ArraySizeModifier ASM, 150 unsigned EltTypeQuals); 151 152 /// getConstantArrayType - Return the unique reference to the type for a 153 /// constant array of the specified element type. 154 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 155 ArrayType::ArraySizeModifier ASM, 156 unsigned EltTypeQuals); 157 158 /// getVectorType - Return the unique reference to a vector type of 159 /// the specified element type and size. VectorType must be a built-in type. 160 QualType getVectorType(QualType VectorType, unsigned NumElts); 161 162 /// getOCUVectorType - Return the unique reference to an OCU vector type of 163 /// the specified element type and size. VectorType must be a built-in type. 164 QualType getOCUVectorType(QualType VectorType, unsigned NumElts); 165 166 /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. 167 /// 168 QualType getFunctionTypeNoProto(QualType ResultTy); 169 170 /// getFunctionType - Return a normal function type with a typed argument 171 /// list. isVariadic indicates whether the argument list includes '...'. 172 QualType getFunctionType(QualType ResultTy, QualType *ArgArray, 173 unsigned NumArgs, bool isVariadic); 174 175 /// getTypedefType - Return the unique reference to the type for the 176 /// specified typename decl. 177 QualType getTypedefType(TypedefDecl *Decl); 178 QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl); 179 180 /// getObjCQualifiedInterfaceType - Return a 181 /// ObjCQualifiedInterfaceType type for the given interface decl and 182 /// the conforming protocol list. 183 QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, 184 ObjCProtocolDecl **ProtocolList, unsigned NumProtocols); 185 186 /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a 187 /// given 'id' and conforming protocol list. 188 QualType getObjCQualifiedIdType(QualType idType, 189 ObjCProtocolDecl **ProtocolList, 190 unsigned NumProtocols); 191 192 193 /// getTypeOfType - GCC extension. 194 QualType getTypeOfExpr(Expr *e); 195 QualType getTypeOfType(QualType t); 196 197 /// getTagDeclType - Return the unique reference to the type for the 198 /// specified TagDecl (struct/union/class/enum) decl. 199 QualType getTagDeclType(TagDecl *Decl); 200 201 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 202 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 203 QualType getSizeType() const; 204 205 /// getWcharType - Return the unique type for "wchar_t" (C99 7.17), defined 206 /// in <stddef.h>. Wide strings require this (C99 6.4.5p5). 207 QualType getWcharType() const; 208 209 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 210 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 211 QualType getPointerDiffType() const; 212 213 // getCFConstantStringType - Return the C structure type used to represent 214 // constant CFStrings. 215 QualType getCFConstantStringType(); 216 217 // This setter/getter represents the ObjC type for an NSConstantString. 218 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 219 QualType getObjCConstantStringInterface() const { 220 return ObjCConstantStringType; 221 } 222 223 // Return the ObjC type encoding for a given type. 224 void getObjCEncodingForType(QualType t, std::string &S, 225 llvm::SmallVector<const RecordType *, 8> &RT) const; 226 227 // Put the string version of type qualifiers into S. 228 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 229 std::string &S) const; 230 231 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 232 /// declaration. 233 void getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl, std::string &S); 234 235 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 236 /// purpose. 237 int getObjCEncodingTypeSize(QualType t); 238 239 // This setter/getter repreents the ObjC 'id' type. It is setup lazily, by 240 // Sema. 241 void setObjCIdType(TypedefDecl *Decl); 242 QualType getObjCIdType() const { return ObjCIdType; } 243 244 void setObjCSelType(TypedefDecl *Decl); 245 QualType getObjCSelType() const { return ObjCSelType; } 246 247 void setObjCProtoType(QualType QT); 248 QualType getObjCProtoType() const { return ObjCProtoType; } 249 250 void setObjCClassType(TypedefDecl *Decl); 251 QualType getObjCClassType() const { return ObjCClassType; } 252 253 void setBuiltinVaListType(QualType T); 254 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 255 256 //===--------------------------------------------------------------------===// 257 // Type Sizing and Analysis 258 //===--------------------------------------------------------------------===// 259 260 /// getTypeInfo - Get the size and alignment of the specified complete type in 261 /// bits. 262 std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L); 263 264 /// getTypeSize - Return the size of the specified type, in bits. This method 265 /// does not work on incomplete types. 266 uint64_t getTypeSize(QualType T, SourceLocation L) { 267 return getTypeInfo(T, L).first; 268 } 269 270 /// getTypeAlign - Return the alignment of the specified type, in bits. This 271 /// method does not work on incomplete types. 272 unsigned getTypeAlign(QualType T, SourceLocation L) { 273 return getTypeInfo(T, L).second; 274 } 275 276 /// getASTRecordLayout - Get or compute information about the layout of the 277 /// specified record (struct/union/class), which indicates its size and field 278 /// position information. 279 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D, SourceLocation L); 280 281 //===--------------------------------------------------------------------===// 282 // Type Operators 283 //===--------------------------------------------------------------------===// 284 285 /// maxIntegerType - Returns the highest ranked integer type. Handles 3 286 /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned. 287 static QualType maxIntegerType(QualType lhs, QualType rhs); 288 289 /// compareFloatingType - Handles 3 different combos: 290 /// float/float, float/complex, complex/complex. 291 /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1. 292 static int compareFloatingType(QualType lt, QualType rt); 293 294 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 295 /// point or a complex type (based on typeDomain/typeSize). 296 /// 'typeDomain' is a real floating point or complex type. 297 /// 'typeSize' is a real floating point or complex type. 298 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 299 QualType typeDomain) const; 300 301 //===--------------------------------------------------------------------===// 302 // Type Compatibility Predicates 303 //===--------------------------------------------------------------------===// 304 305 /// Compatibility predicates used to check assignment expressions. 306 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 307 bool tagTypesAreCompatible(QualType, QualType); // C99 6.2.7p1 308 bool pointerTypesAreCompatible(QualType, QualType); // C99 6.7.5.1p2 309 bool referenceTypesAreCompatible(QualType, QualType); // C++ 5.17p6 310 bool functionTypesAreCompatible(QualType, QualType); // C99 6.7.5.3p15 311 bool arrayTypesAreCompatible(QualType, QualType); // C99 6.7.5.2p6 312 bool builtinTypesAreCompatible(QualType, QualType); 313 bool vectorTypesAreCompatible(QualType, QualType); 314 315 bool QualifiedInterfaceTypesAreCompatible(QualType, QualType); 316 bool ObjCQualifiedIdTypesAreCompatible(QualType, QualType, bool = false); 317 bool objcTypesAreCompatible(QualType, QualType); 318 bool isObjCIdType(QualType T) const { 319 if (!IdStructType) // ObjC isn't enabled 320 return false; 321 return T->getAsStructureType() == IdStructType; 322 } 323 bool isObjCClassType(QualType T) const { 324 if (!ClassStructType) // ObjC isn't enabled 325 return false; 326 return T->getAsStructureType() == ClassStructType; 327 } 328 bool isObjCSelType(QualType T) const { 329 assert(SelStructType && "isObjCSelType used before 'SEL' type is built"); 330 return T->getAsStructureType() == SelStructType; 331 } 332 333private: 334 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 335 void operator=(const ASTContext&); // DO NOT IMPLEMENT 336 337 void InitBuiltinTypes(); 338 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 339 340 /// helper function for Objective-C specific type checking. 341 bool interfaceTypesAreCompatible(QualType, QualType); 342 343 //===--------------------------------------------------------------------===// 344 // Serialization 345 //===--------------------------------------------------------------------===// 346 347public: 348 void Emit(llvm::Serializer& S) const; 349 static ASTContext* Create(llvm::Deserializer& D); 350}; 351 352} // end namespace clang 353 354#endif 355