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