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