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