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