ASTContext.h revision e7d07d113677a39026ff5119b8b67f6fe8ca9793
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/Expr.h" 20#include "clang/AST/RecordLayout.h" 21#include "clang/AST/Type.h" 22#include "llvm/ADT/DenseMap.h" 23#include "llvm/ADT/StringMap.h" 24#include "llvm/ADT/FoldingSet.h" 25#include "llvm/Bitcode/SerializationFwd.h" 26#include "llvm/Support/Allocator.h" 27#include <vector> 28 29namespace clang { 30 class TargetInfo; 31 class IdentifierTable; 32 33/// ASTContext - This class holds long-lived AST nodes (such as types and 34/// decls) that can be referred to throughout the semantic analysis of a file. 35class ASTContext { 36 std::vector<Type*> Types; 37 llvm::FoldingSet<ASQualType> ASQualTypes; 38 llvm::FoldingSet<ComplexType> ComplexTypes; 39 llvm::FoldingSet<PointerType> PointerTypes; 40 llvm::FoldingSet<ReferenceType> ReferenceTypes; 41 llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes; 42 llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes; 43 std::vector<VariableArrayType*> VariableArrayTypes; 44 llvm::FoldingSet<VectorType> VectorTypes; 45 llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos; 46 llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos; 47 llvm::FoldingSet<ObjCQualifiedInterfaceType> ObjCQualifiedInterfaceTypes; 48 llvm::FoldingSet<ObjCQualifiedIdType> ObjCQualifiedIdTypes; 49 /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts. 50 /// This is lazily created. This is intentionally not serialized. 51 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts; 52 53 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes; 54 55 /// BuiltinVaListType - built-in va list type. 56 /// This is initially null and set by Sema::LazilyCreateBuiltin when 57 /// a builtin that takes a valist is encountered. 58 QualType BuiltinVaListType; 59 60 /// ObjCIdType - a pseudo built-in typedef type (set by Sema). 61 QualType ObjCIdType; 62 const RecordType *IdStructType; 63 64 /// ObjCSelType - another pseudo built-in typedef type (set by Sema). 65 QualType ObjCSelType; 66 const RecordType *SelStructType; 67 68 /// ObjCProtoType - another pseudo built-in typedef type (set by Sema). 69 QualType ObjCProtoType; 70 const RecordType *ProtoStructType; 71 72 /// ObjCClassType - another pseudo built-in typedef type (set by Sema). 73 QualType ObjCClassType; 74 const RecordType *ClassStructType; 75 76 QualType ObjCConstantStringType; 77 RecordDecl *CFConstantStringTypeDecl; 78 79 TranslationUnitDecl *TUDecl; 80 81 /// SourceMgr - The associated SourceManager object. 82 SourceManager &SourceMgr; 83 84 /// LangOpts - The language options used to create the AST associated with 85 /// this ASTContext object. 86 LangOptions LangOpts; 87 88 /// Allocator - The allocator object used to create AST objects. 89 llvm::MallocAllocator Allocator; 90 91public: 92 TargetInfo &Target; 93 IdentifierTable &Idents; 94 SelectorTable &Selectors; 95 96 SourceManager& getSourceManager() { return SourceMgr; } 97 llvm::MallocAllocator &getAllocator() { return Allocator; } 98 const LangOptions& getLangOptions() const { return LangOpts; } 99 100 FullSourceLoc getFullLoc(SourceLocation Loc) const { 101 return FullSourceLoc(Loc,SourceMgr); 102 } 103 104 TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; } 105 106 /// This is intentionally not serialized. It is populated by the 107 /// ASTContext ctor, and there are no external pointers/references to 108 /// internal variables of BuiltinInfo. 109 Builtin::Context BuiltinInfo; 110 111 // Builtin Types. 112 QualType VoidTy; 113 QualType BoolTy; 114 QualType CharTy; 115 QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy; 116 QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy; 117 QualType UnsignedLongLongTy; 118 QualType FloatTy, DoubleTy, LongDoubleTy; 119 QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; 120 QualType VoidPtrTy; 121 122 ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t, 123 IdentifierTable &idents, SelectorTable &sels, 124 unsigned size_reserve=0 ) : 125 CFConstantStringTypeDecl(0), SourceMgr(SM), LangOpts(LOpts), Target(t), 126 Idents(idents), Selectors(sels) { 127 128 if (size_reserve > 0) Types.reserve(size_reserve); 129 InitBuiltinTypes(); 130 BuiltinInfo.InitializeBuiltins(idents, Target); 131 TUDecl = TranslationUnitDecl::Create(*this); 132 } 133 134 ~ASTContext(); 135 136 void PrintStats() const; 137 const std::vector<Type*>& getTypes() const { return Types; } 138 139 //===--------------------------------------------------------------------===// 140 // Type Constructors 141 //===--------------------------------------------------------------------===// 142 143 /// getASQualType - Return the uniqued reference to the type for an address 144 /// space qualified type with the specified type and address space. The 145 /// resulting type has a union of the qualifiers from T and the address space. 146 // If T already has an address space specifier, it is silently replaced. 147 QualType getASQualType(QualType T, unsigned AddressSpace); 148 149 /// getComplexType - Return the uniqued reference to the type for a complex 150 /// number with the specified element type. 151 QualType getComplexType(QualType T); 152 153 /// getPointerType - Return the uniqued reference to the type for a pointer to 154 /// the specified type. 155 QualType getPointerType(QualType T); 156 157 /// getReferenceType - Return the uniqued reference to the type for a 158 /// reference to the specified type. 159 QualType getReferenceType(QualType T); 160 161 /// getVariableArrayType - Returns a non-unique reference to the type for a 162 /// variable array of the specified element type. 163 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 164 ArrayType::ArraySizeModifier ASM, 165 unsigned EltTypeQuals); 166 167 /// getIncompleteArrayType - Returns a unique reference to the type for a 168 /// incomplete array of the specified element type. 169 QualType getIncompleteArrayType(QualType EltTy, 170 ArrayType::ArraySizeModifier ASM, 171 unsigned EltTypeQuals); 172 173 /// getConstantArrayType - Return the unique reference to the type for a 174 /// constant array of the specified element type. 175 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 176 ArrayType::ArraySizeModifier ASM, 177 unsigned EltTypeQuals); 178 179 /// getVectorType - Return the unique reference to a vector type of 180 /// the specified element type and size. VectorType must be a built-in type. 181 QualType getVectorType(QualType VectorType, unsigned NumElts); 182 183 /// getExtVectorType - Return the unique reference to an extended vector type 184 /// of the specified element type and size. VectorType must be a built-in 185 /// type. 186 QualType getExtVectorType(QualType VectorType, unsigned NumElts); 187 188 /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. 189 /// 190 QualType getFunctionTypeNoProto(QualType ResultTy); 191 192 /// getFunctionType - Return a normal function type with a typed argument 193 /// list. isVariadic indicates whether the argument list includes '...'. 194 QualType getFunctionType(QualType ResultTy, QualType *ArgArray, 195 unsigned NumArgs, bool isVariadic); 196 197 /// getTypeDeclType - Return the unique reference to the type for 198 /// the specified type declaration. 199 QualType getTypeDeclType(TypeDecl *Decl); 200 201 /// getTypedefType - Return the unique reference to the type for the 202 /// specified typename decl. 203 QualType getTypedefType(TypedefDecl *Decl); 204 QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl); 205 206 /// getObjCQualifiedInterfaceType - Return a 207 /// ObjCQualifiedInterfaceType type for the given interface decl and 208 /// the conforming protocol list. 209 QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, 210 ObjCProtocolDecl **ProtocolList, unsigned NumProtocols); 211 212 /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a 213 /// given 'id' and conforming protocol list. 214 QualType getObjCQualifiedIdType(QualType idType, 215 ObjCProtocolDecl **ProtocolList, 216 unsigned NumProtocols); 217 218 219 /// getTypeOfType - GCC extension. 220 QualType getTypeOfExpr(Expr *e); 221 QualType getTypeOfType(QualType t); 222 223 /// getTagDeclType - Return the unique reference to the type for the 224 /// specified TagDecl (struct/union/class/enum) decl. 225 QualType getTagDeclType(TagDecl *Decl); 226 227 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 228 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 229 QualType getSizeType() const; 230 231 /// getWcharType - Return the unique type for "wchar_t" (C99 7.17), defined 232 /// in <stddef.h>. Wide strings require this (C99 6.4.5p5). 233 QualType getWcharType() const; 234 235 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 236 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 237 QualType getPointerDiffType() const; 238 239 // getCFConstantStringType - Return the C structure type used to represent 240 // constant CFStrings. 241 QualType getCFConstantStringType(); 242 243 // This setter/getter represents the ObjC type for an NSConstantString. 244 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 245 QualType getObjCConstantStringInterface() const { 246 return ObjCConstantStringType; 247 } 248 249 // Return the ObjC type encoding for a given type. 250 void getObjCEncodingForType(QualType t, std::string &S, 251 llvm::SmallVector<const RecordType *, 8> &RT) const; 252 253 // Put the string version of type qualifiers into S. 254 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 255 std::string &S) const; 256 257 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 258 /// declaration. 259 void getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl, std::string &S); 260 261 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 262 /// purpose. 263 int getObjCEncodingTypeSize(QualType t); 264 265 // This setter/getter repreents the ObjC 'id' type. It is setup lazily, by 266 // Sema. 267 void setObjCIdType(TypedefDecl *Decl); 268 QualType getObjCIdType() const { return ObjCIdType; } 269 270 void setObjCSelType(TypedefDecl *Decl); 271 QualType getObjCSelType() const { return ObjCSelType; } 272 273 void setObjCProtoType(QualType QT); 274 QualType getObjCProtoType() const { return ObjCProtoType; } 275 276 void setObjCClassType(TypedefDecl *Decl); 277 QualType getObjCClassType() const { return ObjCClassType; } 278 279 void setBuiltinVaListType(QualType T); 280 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 281 282 //===--------------------------------------------------------------------===// 283 // Type Sizing and Analysis 284 //===--------------------------------------------------------------------===// 285 286 /// getTypeInfo - Get the size and alignment of the specified complete type in 287 /// bits. 288 std::pair<uint64_t, unsigned> getTypeInfo(QualType T); 289 290 /// getTypeSize - Return the size of the specified type, in bits. This method 291 /// does not work on incomplete types. 292 uint64_t getTypeSize(QualType T) { 293 return getTypeInfo(T).first; 294 } 295 296 /// getTypeAlign - Return the alignment of the specified type, in bits. This 297 /// method does not work on incomplete types. 298 unsigned getTypeAlign(QualType T) { 299 return getTypeInfo(T).second; 300 } 301 302 /// getASTRecordLayout - Get or compute information about the layout of the 303 /// specified record (struct/union/class), which indicates its size and field 304 /// position information. 305 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 306 307 //===--------------------------------------------------------------------===// 308 // Type Operators 309 //===--------------------------------------------------------------------===// 310 311 /// getCanonicalType - Return the canonical (structural) type corresponding to 312 /// the specified potentially non-canonical type. The non-canonical version 313 /// of a type may have many "decorated" versions of types. Decorators can 314 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 315 /// to be free of any of these, allowing two canonical types to be compared 316 /// for exact equality with a simple pointer comparison. 317 QualType getCanonicalType(QualType T); 318 319 /// getArrayDecayedType - Return the properly qualified result of decaying the 320 /// specified array type to a pointer. This operation is non-trivial when 321 /// handling typedefs etc. The canonical type of "T" must be an array type, 322 /// this returns a pointer to a properly qualified element of the array. 323 /// 324 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 325 QualType getArrayDecayedType(QualType T); 326 327 /// getIntegerTypeOrder - Returns the highest ranked integer type: 328 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 329 /// LHS < RHS, return -1. 330 int getIntegerTypeOrder(QualType LHS, QualType RHS); 331 332 /// getFloatingTypeOrder - Compare the rank of the two specified floating 333 /// point types, ignoring the domain of the type (i.e. 'double' == 334 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 335 /// LHS < RHS, return -1. 336 int getFloatingTypeOrder(QualType LHS, QualType RHS); 337 338 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 339 /// point or a complex type (based on typeDomain/typeSize). 340 /// 'typeDomain' is a real floating point or complex type. 341 /// 'typeSize' is a real floating point or complex type. 342 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 343 QualType typeDomain) const; 344 345 //===--------------------------------------------------------------------===// 346 // Type Compatibility Predicates 347 //===--------------------------------------------------------------------===// 348 349 /// Compatibility predicates used to check assignment expressions. 350 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 351 bool pointerTypesAreCompatible(QualType, QualType); // C99 6.7.5.1p2 352 bool referenceTypesAreCompatible(QualType, QualType); // C++ 5.17p6 353 bool functionTypesAreCompatible(QualType, QualType); // C99 6.7.5.3p15 354 355 bool isObjCIdType(QualType T) const { 356 if (!IdStructType) // ObjC isn't enabled 357 return false; 358 return T->getAsStructureType() == IdStructType; 359 } 360 bool isObjCClassType(QualType T) const { 361 if (!ClassStructType) // ObjC isn't enabled 362 return false; 363 return T->getAsStructureType() == ClassStructType; 364 } 365 bool isObjCSelType(QualType T) const { 366 assert(SelStructType && "isObjCSelType used before 'SEL' type is built"); 367 return T->getAsStructureType() == SelStructType; 368 } 369 370 //===--------------------------------------------------------------------===// 371 // Serialization 372 //===--------------------------------------------------------------------===// 373 374 void Emit(llvm::Serializer& S) const; 375 static ASTContext* Create(llvm::Deserializer& D); 376 377private: 378 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 379 void operator=(const ASTContext&); // DO NOT IMPLEMENT 380 381 void InitBuiltinTypes(); 382 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 383}; 384 385} // end namespace clang 386 387#endif 388