ASTContext.h revision 971c4fae6092976338b755af1d47dac07c8f16e3
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 unsigned TypeQuals = 0); 213 214 /// getTypeDeclType - Return the unique reference to the type for 215 /// the specified type declaration. 216 QualType getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl=0); 217 218 /// getTypedefType - Return the unique reference to the type for the 219 /// specified typename decl. 220 QualType getTypedefType(TypedefDecl *Decl); 221 QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl); 222 223 /// getObjCQualifiedInterfaceType - Return a 224 /// ObjCQualifiedInterfaceType type for the given interface decl and 225 /// the conforming protocol list. 226 QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, 227 ObjCProtocolDecl **ProtocolList, unsigned NumProtocols); 228 229 /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a 230 /// given 'id' and conforming protocol list. 231 QualType getObjCQualifiedIdType(ObjCProtocolDecl **ProtocolList, 232 unsigned NumProtocols); 233 234 235 /// getTypeOfType - GCC extension. 236 QualType getTypeOfExpr(Expr *e); 237 QualType getTypeOfType(QualType t); 238 239 /// getTagDeclType - Return the unique reference to the type for the 240 /// specified TagDecl (struct/union/class/enum) decl. 241 QualType getTagDeclType(TagDecl *Decl); 242 243 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 244 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 245 QualType getSizeType() const; 246 247 /// getWCharType - Return the unique type for "wchar_t" (C99 7.17), defined 248 /// in <stddef.h>. Wide strings require this (C99 6.4.5p5). 249 QualType getWCharType() const; 250 251 /// getSignedWCharType - Return the type of "signed wchar_t". 252 /// Used when in C++, as a GCC extension. 253 QualType getSignedWCharType() const; 254 255 /// getUnsignedWCharType - Return the type of "unsigned wchar_t". 256 /// Used when in C++, as a GCC extension. 257 QualType getUnsignedWCharType() const; 258 259 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 260 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 261 QualType getPointerDiffType() const; 262 263 // getCFConstantStringType - Return the C structure type used to represent 264 // constant CFStrings. 265 QualType getCFConstantStringType(); 266 267 // This setter/getter represents the ObjC type for an NSConstantString. 268 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 269 QualType getObjCConstantStringInterface() const { 270 return ObjCConstantStringType; 271 } 272 273 //// This gets the struct used to keep track of fast enumerations. 274 QualType getObjCFastEnumerationStateType(); 275 276 /// getObjCEncodingForType - Emit the ObjC type encoding for the 277 /// given type into \arg S. If \arg NameFields is specified then 278 /// record field names are also encoded. 279 void getObjCEncodingForType(QualType t, std::string &S, 280 bool NameFields=false) const; 281 282 // Put the string version of type qualifiers into S. 283 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 284 std::string &S) const; 285 286 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 287 /// declaration. 288 void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S); 289 290 /// getObjCEncodingForPropertyDecl - Return the encoded type for 291 /// this method declaration. If non-NULL, Container must be either 292 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 293 /// only be NULL when getting encodings for protocol properties. 294 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 295 const Decl *Container, 296 std::string &S); 297 298 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 299 /// purpose. 300 int getObjCEncodingTypeSize(QualType t); 301 302 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 303 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 304 QualType getObjCIdType() const { return ObjCIdType; } 305 void setObjCIdType(TypedefDecl *Decl); 306 307 void setObjCSelType(TypedefDecl *Decl); 308 QualType getObjCSelType() const { return ObjCSelType; } 309 310 void setObjCProtoType(QualType QT); 311 QualType getObjCProtoType() const { return ObjCProtoType; } 312 313 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 314 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 315 /// struct. 316 QualType getObjCClassType() const { return ObjCClassType; } 317 void setObjCClassType(TypedefDecl *Decl); 318 319 void setBuiltinVaListType(QualType T); 320 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 321 322 //===--------------------------------------------------------------------===// 323 // Type Predicates. 324 //===--------------------------------------------------------------------===// 325 326 /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer 327 /// to an object type. This includes "id" and "Class" (two 'special' pointers 328 /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified 329 /// ID type). 330 bool isObjCObjectPointerType(QualType Ty) const; 331 332 //===--------------------------------------------------------------------===// 333 // Type Sizing and Analysis 334 //===--------------------------------------------------------------------===// 335 336 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 337 /// scalar floating point type. 338 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 339 340 /// getTypeInfo - Get the size and alignment of the specified complete type in 341 /// bits. 342 std::pair<uint64_t, unsigned> getTypeInfo(QualType T); 343 344 /// getTypeSize - Return the size of the specified type, in bits. This method 345 /// does not work on incomplete types. 346 uint64_t getTypeSize(QualType T) { 347 return getTypeInfo(T).first; 348 } 349 350 /// getTypeAlign - Return the alignment of the specified type, in bits. This 351 /// method does not work on incomplete types. 352 unsigned getTypeAlign(QualType T) { 353 return getTypeInfo(T).second; 354 } 355 356 /// getASTRecordLayout - Get or compute information about the layout of the 357 /// specified record (struct/union/class), which indicates its size and field 358 /// position information. 359 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 360 361 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 362 //===--------------------------------------------------------------------===// 363 // Type Operators 364 //===--------------------------------------------------------------------===// 365 366 /// getCanonicalType - Return the canonical (structural) type corresponding to 367 /// the specified potentially non-canonical type. The non-canonical version 368 /// of a type may have many "decorated" versions of types. Decorators can 369 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 370 /// to be free of any of these, allowing two canonical types to be compared 371 /// for exact equality with a simple pointer comparison. 372 QualType getCanonicalType(QualType T); 373 374 /// Type Query functions. If the type is an instance of the specified class, 375 /// return the Type pointer for the underlying maximally pretty type. This 376 /// is a member of ASTContext because this may need to do some amount of 377 /// canonicalization, e.g. to move type qualifiers into the element type. 378 const ArrayType *getAsArrayType(QualType T); 379 const ConstantArrayType *getAsConstantArrayType(QualType T) { 380 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 381 } 382 const VariableArrayType *getAsVariableArrayType(QualType T) { 383 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 384 } 385 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 386 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 387 } 388 389 /// getArrayDecayedType - Return the properly qualified result of decaying the 390 /// specified array type to a pointer. This operation is non-trivial when 391 /// handling typedefs etc. The canonical type of "T" must be an array type, 392 /// this returns a pointer to a properly qualified element of the array. 393 /// 394 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 395 QualType getArrayDecayedType(QualType T); 396 397 /// getIntegerTypeOrder - Returns the highest ranked integer type: 398 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 399 /// LHS < RHS, return -1. 400 int getIntegerTypeOrder(QualType LHS, QualType RHS); 401 402 /// getFloatingTypeOrder - Compare the rank of the two specified floating 403 /// point types, ignoring the domain of the type (i.e. 'double' == 404 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 405 /// LHS < RHS, return -1. 406 int getFloatingTypeOrder(QualType LHS, QualType RHS); 407 408 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 409 /// point or a complex type (based on typeDomain/typeSize). 410 /// 'typeDomain' is a real floating point or complex type. 411 /// 'typeSize' is a real floating point or complex type. 412 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 413 QualType typeDomain) const; 414 415 //===--------------------------------------------------------------------===// 416 // Type Compatibility Predicates 417 //===--------------------------------------------------------------------===// 418 419 /// Compatibility predicates used to check assignment expressions. 420 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 421 bool typesAreBlockCompatible(QualType lhs, QualType rhs); 422 423 bool isObjCIdType(QualType T) const { 424 if (!IdStructType) // ObjC isn't enabled 425 return false; 426 return T->getAsStructureType() == IdStructType; 427 } 428 bool isObjCClassType(QualType T) const { 429 if (!ClassStructType) // ObjC isn't enabled 430 return false; 431 return T->getAsStructureType() == ClassStructType; 432 } 433 bool isObjCSelType(QualType T) const { 434 assert(SelStructType && "isObjCSelType used before 'SEL' type is built"); 435 return T->getAsStructureType() == SelStructType; 436 } 437 438 // Check the safety of assignment from LHS to RHS 439 bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 440 const ObjCInterfaceType *RHS); 441 442 // Functions for calculating composite types 443 QualType mergeTypes(QualType, QualType); 444 QualType mergeFunctionTypes(QualType, QualType); 445 446 //===--------------------------------------------------------------------===// 447 // Integer Predicates 448 //===--------------------------------------------------------------------===// 449 450 // The width of an integer, as defined in C99 6.2.6.2. This is the number 451 // of bits in an integer type excluding any padding bits. 452 unsigned getIntWidth(QualType T); 453 454 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 455 // unsigned integer type. This method takes a signed type, and returns the 456 // corresponding unsigned integer type. 457 QualType getCorrespondingUnsignedType(QualType T); 458 459 //===--------------------------------------------------------------------===// 460 // Type Iterators. 461 //===--------------------------------------------------------------------===// 462 463 typedef std::vector<Type*>::iterator type_iterator; 464 typedef std::vector<Type*>::const_iterator const_type_iterator; 465 466 type_iterator types_begin() { return Types.begin(); } 467 type_iterator types_end() { return Types.end(); } 468 const_type_iterator types_begin() const { return Types.begin(); } 469 const_type_iterator types_end() const { return Types.end(); } 470 471 //===--------------------------------------------------------------------===// 472 // Serialization 473 //===--------------------------------------------------------------------===// 474 475 void Emit(llvm::Serializer& S) const; 476 static ASTContext* Create(llvm::Deserializer& D); 477 478private: 479 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 480 void operator=(const ASTContext&); // DO NOT IMPLEMENT 481 482 void InitBuiltinTypes(); 483 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 484 485 /// setRecordDefinition - Used by RecordDecl::defineBody to inform ASTContext 486 /// about which RecordDecl serves as the definition of a particular 487 /// struct/union/class. This will eventually be used by enums as well. 488 void setTagDefinition(TagDecl* R); 489 friend class RecordDecl; 490 491 // Return the ObjC type encoding for a given type. 492 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 493 bool ExpandPointedToStructures, 494 bool ExpandStructures, 495 bool NameFields) const; 496 497}; 498 499} // end namespace clang 500 501#endif 502