ASTContext.h revision 77a49f6b1e5741f6bda71eb7837b43343925027b
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 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes; 72 73 /// BuiltinVaListType - built-in va list type. 74 /// This is initially null and set by Sema::LazilyCreateBuiltin when 75 /// a builtin that takes a valist is encountered. 76 QualType BuiltinVaListType; 77 78 /// ObjCIdType - a pseudo built-in typedef type (set by Sema). 79 QualType ObjCIdType; 80 const RecordType *IdStructType; 81 82 /// ObjCSelType - another pseudo built-in typedef type (set by Sema). 83 QualType ObjCSelType; 84 const RecordType *SelStructType; 85 86 /// ObjCProtoType - another pseudo built-in typedef type (set by Sema). 87 QualType ObjCProtoType; 88 const RecordType *ProtoStructType; 89 90 /// ObjCClassType - another pseudo built-in typedef type (set by Sema). 91 QualType ObjCClassType; 92 const RecordType *ClassStructType; 93 94 QualType ObjCConstantStringType; 95 RecordDecl *CFConstantStringTypeDecl; 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); 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 // Return the ObjC type encoding for a given type. 272 void getObjCEncodingForType(QualType t, std::string &S, 273 llvm::SmallVector<const RecordType*,8> &RT) const; 274 275 // Put the string version of type qualifiers into S. 276 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 277 std::string &S) const; 278 279 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 280 /// declaration. 281 void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S); 282 283 /// getObjCEncodingForPropertyDecl - Return the encoded type for 284 /// this method declaration. If non-NULL, Container must be either 285 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 286 /// only be NULL when getting encodings for protocol properties. 287 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 288 const Decl *Container, 289 std::string &S); 290 291 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 292 /// purpose. 293 int getObjCEncodingTypeSize(QualType t); 294 295 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 296 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 297 QualType getObjCIdType() const { return ObjCIdType; } 298 void setObjCIdType(TypedefDecl *Decl); 299 300 void setObjCSelType(TypedefDecl *Decl); 301 QualType getObjCSelType() const { return ObjCSelType; } 302 303 void setObjCProtoType(QualType QT); 304 QualType getObjCProtoType() const { return ObjCProtoType; } 305 306 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 307 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 308 /// struct. 309 QualType getObjCClassType() const { return ObjCClassType; } 310 void setObjCClassType(TypedefDecl *Decl); 311 312 void setBuiltinVaListType(QualType T); 313 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 314 315 //===--------------------------------------------------------------------===// 316 // Type Predicates. 317 //===--------------------------------------------------------------------===// 318 319 /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer 320 /// to an object type. This includes "id" and "Class" (two 'special' pointers 321 /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified 322 /// ID type). 323 bool isObjCObjectPointerType(QualType Ty) const; 324 325 //===--------------------------------------------------------------------===// 326 // Type Sizing and Analysis 327 //===--------------------------------------------------------------------===// 328 329 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 330 /// scalar floating point type. 331 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 332 333 /// getTypeInfo - Get the size and alignment of the specified complete type in 334 /// bits. 335 std::pair<uint64_t, unsigned> getTypeInfo(QualType T); 336 337 /// getTypeSize - Return the size of the specified type, in bits. This method 338 /// does not work on incomplete types. 339 uint64_t getTypeSize(QualType T) { 340 return getTypeInfo(T).first; 341 } 342 343 /// getTypeAlign - Return the alignment of the specified type, in bits. This 344 /// method does not work on incomplete types. 345 unsigned getTypeAlign(QualType T) { 346 return getTypeInfo(T).second; 347 } 348 349 /// getASTRecordLayout - Get or compute information about the layout of the 350 /// specified record (struct/union/class), which indicates its size and field 351 /// position information. 352 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 353 354 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 355 //===--------------------------------------------------------------------===// 356 // Type Operators 357 //===--------------------------------------------------------------------===// 358 359 /// getCanonicalType - Return the canonical (structural) type corresponding to 360 /// the specified potentially non-canonical type. The non-canonical version 361 /// of a type may have many "decorated" versions of types. Decorators can 362 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 363 /// to be free of any of these, allowing two canonical types to be compared 364 /// for exact equality with a simple pointer comparison. 365 QualType getCanonicalType(QualType T); 366 367 /// Type Query functions. If the type is an instance of the specified class, 368 /// return the Type pointer for the underlying maximally pretty type. This 369 /// is a member of ASTContext because this may need to do some amount of 370 /// canonicalization, e.g. to move type qualifiers into the element type. 371 const ArrayType *getAsArrayType(QualType T); 372 const ConstantArrayType *getAsConstantArrayType(QualType T) { 373 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 374 } 375 const VariableArrayType *getAsVariableArrayType(QualType T) { 376 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 377 } 378 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 379 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 380 } 381 382 /// getArrayDecayedType - Return the properly qualified result of decaying the 383 /// specified array type to a pointer. This operation is non-trivial when 384 /// handling typedefs etc. The canonical type of "T" must be an array type, 385 /// this returns a pointer to a properly qualified element of the array. 386 /// 387 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 388 QualType getArrayDecayedType(QualType T); 389 390 /// getIntegerTypeOrder - Returns the highest ranked integer type: 391 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 392 /// LHS < RHS, return -1. 393 int getIntegerTypeOrder(QualType LHS, QualType RHS); 394 395 /// getFloatingTypeOrder - Compare the rank of the two specified floating 396 /// point types, ignoring the domain of the type (i.e. 'double' == 397 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 398 /// LHS < RHS, return -1. 399 int getFloatingTypeOrder(QualType LHS, QualType RHS); 400 401 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 402 /// point or a complex type (based on typeDomain/typeSize). 403 /// 'typeDomain' is a real floating point or complex type. 404 /// 'typeSize' is a real floating point or complex type. 405 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 406 QualType typeDomain) const; 407 408 //===--------------------------------------------------------------------===// 409 // Type Compatibility Predicates 410 //===--------------------------------------------------------------------===// 411 412 /// Compatibility predicates used to check assignment expressions. 413 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 414 415 bool isObjCIdType(QualType T) const { 416 if (!IdStructType) // ObjC isn't enabled 417 return false; 418 return T->getAsStructureType() == IdStructType; 419 } 420 bool isObjCClassType(QualType T) const { 421 if (!ClassStructType) // ObjC isn't enabled 422 return false; 423 return T->getAsStructureType() == ClassStructType; 424 } 425 bool isObjCSelType(QualType T) const { 426 assert(SelStructType && "isObjCSelType used before 'SEL' type is built"); 427 return T->getAsStructureType() == SelStructType; 428 } 429 430 // Check the safety of assignment from LHS to RHS 431 bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 432 const ObjCInterfaceType *RHS); 433 434 // Functions for calculating composite types 435 QualType mergeTypes(QualType, QualType); 436 QualType mergeFunctionTypes(QualType, QualType); 437 438 //===--------------------------------------------------------------------===// 439 // Integer Predicates 440 //===--------------------------------------------------------------------===// 441 442 // The width of an integer, as defined in C99 6.2.6.2. This is the number 443 // of bits in an integer type excluding any padding bits. 444 unsigned getIntWidth(QualType T); 445 446 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 447 // unsigned integer type. This method takes a signed type, and returns the 448 // corresponding unsigned integer type. 449 QualType getCorrespondingUnsignedType(QualType T); 450 451 //===--------------------------------------------------------------------===// 452 // Serialization 453 //===--------------------------------------------------------------------===// 454 455 void Emit(llvm::Serializer& S) const; 456 static ASTContext* Create(llvm::Deserializer& D); 457 458private: 459 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 460 void operator=(const ASTContext&); // DO NOT IMPLEMENT 461 462 void InitBuiltinTypes(); 463 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 464}; 465 466} // end namespace clang 467 468#endif 469