ASTContext.h revision 62f5f7ffad57e0c2af2b308af3735351505937cb
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/Basic/LangOptions.h" 18#include "clang/AST/Builtins.h" 19#include "clang/AST/Expr.h" 20#include "clang/AST/RecordLayout.h" 21#include "clang/AST/Type.h" 22#include "llvm/ADT/DenseMap.h" 23#include "llvm/ADT/StringMap.h" 24#include "llvm/ADT/FoldingSet.h" 25#include "llvm/Bitcode/SerializationFwd.h" 26#include "llvm/Support/Allocator.h" 27#include <vector> 28 29namespace llvm { 30 struct fltSemantics; 31} 32 33namespace clang { 34 class TargetInfo; 35 class IdentifierTable; 36 37/// ASTContext - This class holds long-lived AST nodes (such as types and 38/// decls) that can be referred to throughout the semantic analysis of a file. 39class ASTContext { 40 std::vector<Type*> Types; 41 llvm::FoldingSet<ASQualType> ASQualTypes; 42 llvm::FoldingSet<ComplexType> ComplexTypes; 43 llvm::FoldingSet<PointerType> PointerTypes; 44 llvm::FoldingSet<ReferenceType> ReferenceTypes; 45 llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes; 46 llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes; 47 std::vector<VariableArrayType*> VariableArrayTypes; 48 llvm::FoldingSet<VectorType> VectorTypes; 49 llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos; 50 llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos; 51 llvm::FoldingSet<ObjCQualifiedInterfaceType> ObjCQualifiedInterfaceTypes; 52 llvm::FoldingSet<ObjCQualifiedIdType> ObjCQualifiedIdTypes; 53 /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts. 54 /// This is lazily created. This is intentionally not serialized. 55 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts; 56 llvm::DenseMap<const ObjCInterfaceDecl*, 57 const ASTRecordLayout*> ASTObjCInterfaces; 58 59 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes; 60 61 /// BuiltinVaListType - built-in va list type. 62 /// This is initially null and set by Sema::LazilyCreateBuiltin when 63 /// a builtin that takes a valist is encountered. 64 QualType BuiltinVaListType; 65 66 /// ObjCIdType - a pseudo built-in typedef type (set by Sema). 67 QualType ObjCIdType; 68 const RecordType *IdStructType; 69 70 /// ObjCSelType - another pseudo built-in typedef type (set by Sema). 71 QualType ObjCSelType; 72 const RecordType *SelStructType; 73 74 /// ObjCProtoType - another pseudo built-in typedef type (set by Sema). 75 QualType ObjCProtoType; 76 const RecordType *ProtoStructType; 77 78 /// ObjCClassType - another pseudo built-in typedef type (set by Sema). 79 QualType ObjCClassType; 80 const RecordType *ClassStructType; 81 82 QualType ObjCConstantStringType; 83 RecordDecl *CFConstantStringTypeDecl; 84 85 TranslationUnitDecl *TUDecl; 86 87 /// SourceMgr - The associated SourceManager object. 88 SourceManager &SourceMgr; 89 90 /// LangOpts - The language options used to create the AST associated with 91 /// this ASTContext object. 92 LangOptions LangOpts; 93 94 /// Allocator - The allocator object used to create AST objects. 95 llvm::MallocAllocator Allocator; 96 97public: 98 TargetInfo &Target; 99 IdentifierTable &Idents; 100 SelectorTable &Selectors; 101 102 SourceManager& getSourceManager() { return SourceMgr; } 103 llvm::MallocAllocator &getAllocator() { return Allocator; } 104 const LangOptions& getLangOptions() const { return LangOpts; } 105 106 FullSourceLoc getFullLoc(SourceLocation Loc) const { 107 return FullSourceLoc(Loc,SourceMgr); 108 } 109 110 TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; } 111 112 /// This is intentionally not serialized. It is populated by the 113 /// ASTContext ctor, and there are no external pointers/references to 114 /// internal variables of BuiltinInfo. 115 Builtin::Context BuiltinInfo; 116 117 // Builtin Types. 118 QualType VoidTy; 119 QualType BoolTy; 120 QualType CharTy; 121 QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy; 122 QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy; 123 QualType UnsignedLongLongTy; 124 QualType FloatTy, DoubleTy, LongDoubleTy; 125 QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; 126 QualType VoidPtrTy; 127 128 ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t, 129 IdentifierTable &idents, SelectorTable &sels, 130 unsigned size_reserve=0 ) : 131 CFConstantStringTypeDecl(0), SourceMgr(SM), LangOpts(LOpts), Target(t), 132 Idents(idents), Selectors(sels) { 133 134 if (size_reserve > 0) Types.reserve(size_reserve); 135 InitBuiltinTypes(); 136 BuiltinInfo.InitializeBuiltins(idents, Target); 137 TUDecl = TranslationUnitDecl::Create(*this); 138 } 139 140 ~ASTContext(); 141 142 void PrintStats() const; 143 const std::vector<Type*>& getTypes() const { return Types; } 144 145 //===--------------------------------------------------------------------===// 146 // Type Constructors 147 //===--------------------------------------------------------------------===// 148 149 /// getASQualType - Return the uniqued reference to the type for an address 150 /// space qualified type with the specified type and address space. The 151 /// resulting type has a union of the qualifiers from T and the address space. 152 // If T already has an address space specifier, it is silently replaced. 153 QualType getASQualType(QualType T, unsigned AddressSpace); 154 155 /// getComplexType - Return the uniqued reference to the type for a complex 156 /// number with the specified element type. 157 QualType getComplexType(QualType T); 158 159 /// getPointerType - Return the uniqued reference to the type for a pointer to 160 /// the specified type. 161 QualType getPointerType(QualType T); 162 163 /// getReferenceType - Return the uniqued reference to the type for a 164 /// reference to the specified type. 165 QualType getReferenceType(QualType T); 166 167 /// getVariableArrayType - Returns a non-unique reference to the type for a 168 /// variable array of the specified element type. 169 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 170 ArrayType::ArraySizeModifier ASM, 171 unsigned EltTypeQuals); 172 173 /// getIncompleteArrayType - Returns a unique reference to the type for a 174 /// incomplete array of the specified element type. 175 QualType getIncompleteArrayType(QualType EltTy, 176 ArrayType::ArraySizeModifier ASM, 177 unsigned EltTypeQuals); 178 179 /// getConstantArrayType - Return the unique reference to the type for a 180 /// constant array of the specified element type. 181 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 182 ArrayType::ArraySizeModifier ASM, 183 unsigned EltTypeQuals); 184 185 /// getVectorType - Return the unique reference to a vector type of 186 /// the specified element type and size. VectorType must be a built-in type. 187 QualType getVectorType(QualType VectorType, unsigned NumElts); 188 189 /// getExtVectorType - Return the unique reference to an extended vector type 190 /// of the specified element type and size. VectorType must be a built-in 191 /// type. 192 QualType getExtVectorType(QualType VectorType, unsigned NumElts); 193 194 /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. 195 /// 196 QualType getFunctionTypeNoProto(QualType ResultTy); 197 198 /// getFunctionType - Return a normal function type with a typed argument 199 /// list. isVariadic indicates whether the argument list includes '...'. 200 QualType getFunctionType(QualType ResultTy, QualType *ArgArray, 201 unsigned NumArgs, bool isVariadic); 202 203 /// getTypeDeclType - Return the unique reference to the type for 204 /// the specified type declaration. 205 QualType getTypeDeclType(TypeDecl *Decl); 206 207 /// getTypedefType - Return the unique reference to the type for the 208 /// specified typename decl. 209 QualType getTypedefType(TypedefDecl *Decl); 210 QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl); 211 212 /// getObjCQualifiedInterfaceType - Return a 213 /// ObjCQualifiedInterfaceType type for the given interface decl and 214 /// the conforming protocol list. 215 QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, 216 ObjCProtocolDecl **ProtocolList, unsigned NumProtocols); 217 218 /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a 219 /// given 'id' and conforming protocol list. 220 QualType getObjCQualifiedIdType(ObjCProtocolDecl **ProtocolList, 221 unsigned NumProtocols); 222 223 224 /// getTypeOfType - GCC extension. 225 QualType getTypeOfExpr(Expr *e); 226 QualType getTypeOfType(QualType t); 227 228 /// getTagDeclType - Return the unique reference to the type for the 229 /// specified TagDecl (struct/union/class/enum) decl. 230 QualType getTagDeclType(TagDecl *Decl); 231 232 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 233 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 234 QualType getSizeType() const; 235 236 /// getWcharType - Return the unique type for "wchar_t" (C99 7.17), defined 237 /// in <stddef.h>. Wide strings require this (C99 6.4.5p5). 238 QualType getWcharType() const; 239 240 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 241 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 242 QualType getPointerDiffType() const; 243 244 // getCFConstantStringType - Return the C structure type used to represent 245 // constant CFStrings. 246 QualType getCFConstantStringType(); 247 248 // This setter/getter represents the ObjC type for an NSConstantString. 249 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 250 QualType getObjCConstantStringInterface() const { 251 return ObjCConstantStringType; 252 } 253 254 // Return the ObjC type encoding for a given type. 255 void getObjCEncodingForType(QualType t, std::string &S, 256 llvm::SmallVector<const RecordType *, 8> &RT) const; 257 258 // Put the string version of type qualifiers into S. 259 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 260 std::string &S) const; 261 262 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 263 /// declaration. 264 void getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl, std::string &S); 265 266 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 267 /// purpose. 268 int getObjCEncodingTypeSize(QualType t); 269 270 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 271 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 272 QualType getObjCIdType() const { return ObjCIdType; } 273 void setObjCIdType(TypedefDecl *Decl); 274 275 void setObjCSelType(TypedefDecl *Decl); 276 QualType getObjCSelType() const { return ObjCSelType; } 277 278 void setObjCProtoType(QualType QT); 279 QualType getObjCProtoType() const { return ObjCProtoType; } 280 281 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 282 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 283 /// struct. 284 QualType getObjCClassType() const { return ObjCClassType; } 285 void setObjCClassType(TypedefDecl *Decl); 286 287 void setBuiltinVaListType(QualType T); 288 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 289 290 //===--------------------------------------------------------------------===// 291 // Type Predicates. 292 //===--------------------------------------------------------------------===// 293 294 /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer 295 /// to an object type. This includes "id" and "Class" (two 'special' pointers 296 /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified 297 /// ID type). 298 bool isObjCObjectPointerType(QualType Ty) const; 299 300 //===--------------------------------------------------------------------===// 301 // Type Sizing and Analysis 302 //===--------------------------------------------------------------------===// 303 304 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 305 /// scalar floating point type. 306 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 307 308 /// getTypeInfo - Get the size and alignment of the specified complete type in 309 /// bits. 310 std::pair<uint64_t, unsigned> getTypeInfo(QualType T); 311 312 /// getTypeSize - Return the size of the specified type, in bits. This method 313 /// does not work on incomplete types. 314 uint64_t getTypeSize(QualType T) { 315 return getTypeInfo(T).first; 316 } 317 318 /// getTypeAlign - Return the alignment of the specified type, in bits. This 319 /// method does not work on incomplete types. 320 unsigned getTypeAlign(QualType T) { 321 return getTypeInfo(T).second; 322 } 323 324 /// getASTRecordLayout - Get or compute information about the layout of the 325 /// specified record (struct/union/class), which indicates its size and field 326 /// position information. 327 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 328 329 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 330 //===--------------------------------------------------------------------===// 331 // Type Operators 332 //===--------------------------------------------------------------------===// 333 334 /// getCanonicalType - Return the canonical (structural) type corresponding to 335 /// the specified potentially non-canonical type. The non-canonical version 336 /// of a type may have many "decorated" versions of types. Decorators can 337 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 338 /// to be free of any of these, allowing two canonical types to be compared 339 /// for exact equality with a simple pointer comparison. 340 QualType getCanonicalType(QualType T); 341 342 /// getArrayDecayedType - Return the properly qualified result of decaying the 343 /// specified array type to a pointer. This operation is non-trivial when 344 /// handling typedefs etc. The canonical type of "T" must be an array type, 345 /// this returns a pointer to a properly qualified element of the array. 346 /// 347 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 348 QualType getArrayDecayedType(QualType T); 349 350 /// getIntegerTypeOrder - Returns the highest ranked integer type: 351 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 352 /// LHS < RHS, return -1. 353 int getIntegerTypeOrder(QualType LHS, QualType RHS); 354 355 /// getFloatingTypeOrder - Compare the rank of the two specified floating 356 /// point types, ignoring the domain of the type (i.e. 'double' == 357 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 358 /// LHS < RHS, return -1. 359 int getFloatingTypeOrder(QualType LHS, QualType RHS); 360 361 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 362 /// point or a complex type (based on typeDomain/typeSize). 363 /// 'typeDomain' is a real floating point or complex type. 364 /// 'typeSize' is a real floating point or complex type. 365 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 366 QualType typeDomain) const; 367 368 //===--------------------------------------------------------------------===// 369 // Type Compatibility Predicates 370 //===--------------------------------------------------------------------===// 371 372 /// Compatibility predicates used to check assignment expressions. 373 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 374 bool pointerTypesAreCompatible(QualType, QualType); // C99 6.7.5.1p2 375 bool referenceTypesAreCompatible(QualType, QualType); // C++ 5.17p6 376 bool functionTypesAreCompatible(QualType, QualType); // C99 6.7.5.3p15 377 378 bool isObjCIdType(QualType T) const { 379 if (!IdStructType) // ObjC isn't enabled 380 return false; 381 return T->getAsStructureType() == IdStructType; 382 } 383 bool isObjCClassType(QualType T) const { 384 if (!ClassStructType) // ObjC isn't enabled 385 return false; 386 return T->getAsStructureType() == ClassStructType; 387 } 388 bool isObjCSelType(QualType T) const { 389 assert(SelStructType && "isObjCSelType used before 'SEL' type is built"); 390 return T->getAsStructureType() == SelStructType; 391 } 392 393 //===--------------------------------------------------------------------===// 394 // Integer Predicates 395 //===--------------------------------------------------------------------===// 396 397 // The width of an integer, as defined in C99 6.2.6.2. This is the number 398 // of bits in an integer type excluding any padding bits. 399 unsigned getIntWidth(QualType T); 400 401 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 402 // unsigned integer type. This method takes a signed type, and returns the 403 // corresponding unsigned integer type. 404 QualType getCorrespondingUnsignedType(QualType T); 405 406 //===--------------------------------------------------------------------===// 407 // Serialization 408 //===--------------------------------------------------------------------===// 409 410 void Emit(llvm::Serializer& S) const; 411 static ASTContext* Create(llvm::Deserializer& D); 412 413private: 414 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 415 void operator=(const ASTContext&); // DO NOT IMPLEMENT 416 417 void InitBuiltinTypes(); 418 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 419}; 420 421} // end namespace clang 422 423#endif 424