ASTContext.h revision 9c728dc4d8da89c73fcae74c9e72d7a83ffd7b6d
1//===--- ASTContext.h - Context to hold long-lived AST nodes ----*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by Chris Lattner and is distributed under 6// the University of Illinois Open Source 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/AST/Builtins.h" 18#include "clang/AST/Expr.h" 19#include "clang/AST/RecordLayout.h" 20#include "clang/AST/Type.h" 21#include "llvm/ADT/DenseMap.h" 22#include "llvm/ADT/StringMap.h" 23#include "llvm/ADT/FoldingSet.h" 24#include "llvm/Bitcode/SerializationFwd.h" 25#include <vector> 26 27namespace clang { 28 class TargetInfo; 29 class IdentifierTable; 30 31/// ASTContext - This class holds long-lived AST nodes (such as types and 32/// decls) that can be referred to throughout the semantic analysis of a file. 33class ASTContext { 34 std::vector<Type*> Types; 35 llvm::FoldingSet<ComplexType> ComplexTypes; 36 llvm::FoldingSet<PointerType> PointerTypes; 37 llvm::FoldingSet<ReferenceType> ReferenceTypes; 38 llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes; 39 llvm::FoldingSet<VariableArrayType> IncompleteVariableArrayTypes; 40 std::vector<VariableArrayType*> CompleteVariableArrayTypes; 41 llvm::FoldingSet<VectorType> VectorTypes; 42 llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos; 43 llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos; 44 llvm::FoldingSet<ObjcQualifiedInterfaceType> ObjcQualifiedInterfaceTypes; 45 /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts. 46 /// This is lazily created. This is intentionally not serialized. 47 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts; 48 49 /// BuiltinVaListType - built-in va list type. 50 /// This is initially null and set by Sema::LazilyCreateBuiltin when 51 /// a builtin that takes a valist is encountered. 52 QualType BuiltinVaListType; 53 54 /// ObjcIdType - a psuedo built-in typedef type (set by Sema). 55 QualType ObjcIdType; 56 const RecordType *IdStructType; 57 58 /// ObjcSelType - another psuedo built-in typedef type (set by Sema). 59 QualType ObjcSelType; 60 const RecordType *SelStructType; 61 62 /// ObjcProtoType - another psuedo built-in typedef type (set by Sema). 63 QualType ObjcProtoType; 64 const RecordType *ProtoStructType; 65 66 /// ObjcClassType - another psuedo built-in typedef type (set by Sema). 67 QualType ObjcClassType; 68 const RecordType *ClassStructType; 69 70 QualType ObjcConstantStringType; 71 RecordDecl *CFConstantStringTypeDecl; 72 73 SourceManager &SourceMgr; 74public: 75 TargetInfo &Target; 76 IdentifierTable &Idents; 77 SelectorTable &Selectors; 78 79 SourceManager& getSourceManager() { return SourceMgr; } 80 81 FullSourceLoc getFullLoc(SourceLocation Loc) const { 82 return FullSourceLoc(Loc,SourceMgr); 83 } 84 85 /// This is intentionally not serialized. It is populated by the 86 /// ASTContext ctor, and there are no external pointers/references to 87 /// internal variables of BuiltinInfo. 88 Builtin::Context BuiltinInfo; 89 90 // Builtin Types. 91 QualType VoidTy; 92 QualType BoolTy; 93 QualType CharTy; 94 QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy; 95 QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy; 96 QualType UnsignedLongLongTy; 97 QualType FloatTy, DoubleTy, LongDoubleTy; 98 QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; 99 QualType VoidPtrTy; 100 101 ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents, 102 SelectorTable &sels, unsigned size_reserve=0 ) : 103 CFConstantStringTypeDecl(0), SourceMgr(SM), Target(t), 104 Idents(idents), Selectors(sels) { 105 106 if (size_reserve > 0) Types.reserve(size_reserve); 107 InitBuiltinTypes(); 108 BuiltinInfo.InitializeBuiltins(idents, Target); 109 } 110 111 ~ASTContext(); 112 113 void PrintStats() const; 114 const std::vector<Type*>& getTypes() const { return Types; } 115 116 //===--------------------------------------------------------------------===// 117 // Type Constructors 118 //===--------------------------------------------------------------------===// 119 120 /// getComplexType - Return the uniqued reference to the type for a complex 121 /// number with the specified element type. 122 QualType getComplexType(QualType T); 123 124 /// getPointerType - Return the uniqued reference to the type for a pointer to 125 /// the specified type. 126 QualType getPointerType(QualType T); 127 128 /// getReferenceType - Return the uniqued reference to the type for a 129 /// reference to the specified type. 130 QualType getReferenceType(QualType T); 131 132 /// getVariableArrayType - Returns a non-unique reference to the type for a 133 /// variable array of the specified element type. 134 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 135 ArrayType::ArraySizeModifier ASM, 136 unsigned EltTypeQuals); 137 138 /// getConstantArrayType - Return the unique reference to the type for a 139 /// constant array of the specified element type. 140 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 141 ArrayType::ArraySizeModifier ASM, 142 unsigned EltTypeQuals); 143 144 /// getVectorType - Return the unique reference to a vector type of 145 /// the specified element type and size. VectorType must be a built-in type. 146 QualType getVectorType(QualType VectorType, unsigned NumElts); 147 148 /// getOCUVectorType - Return the unique reference to an OCU vector type of 149 /// the specified element type and size. VectorType must be a built-in type. 150 QualType getOCUVectorType(QualType VectorType, unsigned NumElts); 151 152 /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. 153 /// 154 QualType getFunctionTypeNoProto(QualType ResultTy); 155 156 /// getFunctionType - Return a normal function type with a typed argument 157 /// list. isVariadic indicates whether the argument list includes '...'. 158 QualType getFunctionType(QualType ResultTy, QualType *ArgArray, 159 unsigned NumArgs, bool isVariadic); 160 161 /// getTypedefType - Return the unique reference to the type for the 162 /// specified typename decl. 163 QualType getTypedefType(TypedefDecl *Decl); 164 QualType getObjcInterfaceType(ObjcInterfaceDecl *Decl); 165 166 /// getObjcQualifiedInterfaceType - Return a 167 /// ObjcQualifiedInterfaceType type for the given interface decl and 168 /// the conforming protocol list. 169 QualType getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl, 170 ObjcProtocolDecl **ProtocolList, unsigned NumProtocols); 171 172 /// getTypeOfType - GCC extension. 173 QualType getTypeOfExpr(Expr *e); 174 QualType getTypeOfType(QualType t); 175 176 /// getTagDeclType - Return the unique reference to the type for the 177 /// specified TagDecl (struct/union/class/enum) decl. 178 QualType getTagDeclType(TagDecl *Decl); 179 180 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 181 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 182 QualType getSizeType() const; 183 184 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 185 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 186 QualType getPointerDiffType() const; 187 188 // getCFConstantStringType - Return the C structure type used to represent 189 // constant CFStrings. 190 QualType getCFConstantStringType(); 191 192 // This setter/getter represents the ObjC type for an NSConstantString. 193 void setObjcConstantStringInterface(ObjcInterfaceDecl *Decl); 194 QualType getObjcConstantStringInterface() const { 195 return ObjcConstantStringType; 196 } 197 198 // Return the ObjC type encoding for a given type. 199 void getObjcEncodingForType(QualType t, std::string &S) const; 200 201 // Put the string version of type qualifiers into S. 202 void getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT, 203 std::string &S) const; 204 205 /// getObjcEncodingForMethodDecl - Return the encoded type for this method 206 /// declaration. 207 void getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl, std::string &S); 208 209 /// getObjcEncodingTypeSize returns size of type for objective-c encoding 210 /// purpose. 211 int getObjcEncodingTypeSize(QualType t); 212 213 // This setter/getter repreents the ObjC 'id' type. It is setup lazily, by 214 // Sema. 215 void setObjcIdType(TypedefDecl *Decl); 216 QualType getObjcIdType() const { return ObjcIdType; } 217 218 void setObjcSelType(TypedefDecl *Decl); 219 QualType getObjcSelType() const { return ObjcSelType; } 220 221 void setObjcProtoType(QualType QT); 222 QualType getObjcProtoType() const { return ObjcProtoType; } 223 224 void setObjcClassType(TypedefDecl *Decl); 225 QualType getObjcClassType() const { return ObjcClassType; } 226 227 void setBuiltinVaListType(QualType T); 228 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 229 230 //===--------------------------------------------------------------------===// 231 // Type Sizing and Analysis 232 //===--------------------------------------------------------------------===// 233 234 /// getTypeInfo - Get the size and alignment of the specified complete type in 235 /// bits. 236 std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L); 237 238 /// getTypeSize - Return the size of the specified type, in bits. This method 239 /// does not work on incomplete types. 240 uint64_t getTypeSize(QualType T, SourceLocation L) { 241 return getTypeInfo(T, L).first; 242 } 243 244 /// getTypeAlign - Return the alignment of the specified type, in bits. This 245 /// method does not work on incomplete types. 246 unsigned getTypeAlign(QualType T, SourceLocation L) { 247 return getTypeInfo(T, L).second; 248 } 249 250 /// getASTRecordLayout - Get or compute information about the layout of the 251 /// specified record (struct/union/class), which indicates its size and field 252 /// position information. 253 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D, SourceLocation L); 254 255 //===--------------------------------------------------------------------===// 256 // Type Operators 257 //===--------------------------------------------------------------------===// 258 259 /// maxIntegerType - Returns the highest ranked integer type. Handles 3 260 /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned. 261 static QualType maxIntegerType(QualType lhs, QualType rhs); 262 263 /// compareFloatingType - Handles 3 different combos: 264 /// float/float, float/complex, complex/complex. 265 /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1. 266 static int compareFloatingType(QualType lt, QualType rt); 267 268 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 269 /// point or a complex type (based on typeDomain/typeSize). 270 /// 'typeDomain' is a real floating point or complex type. 271 /// 'typeSize' is a real floating point or complex type. 272 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 273 QualType typeDomain) const; 274 275 //===--------------------------------------------------------------------===// 276 // Type Compatibility Predicates 277 //===--------------------------------------------------------------------===// 278 279 /// Compatibility predicates used to check assignment expressions. 280 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 281 bool tagTypesAreCompatible(QualType, QualType); // C99 6.2.7p1 282 bool pointerTypesAreCompatible(QualType, QualType); // C99 6.7.5.1p2 283 bool referenceTypesAreCompatible(QualType, QualType); // C++ 5.17p6 284 bool functionTypesAreCompatible(QualType, QualType); // C99 6.7.5.3p15 285 bool arrayTypesAreCompatible(QualType, QualType); // C99 6.7.5.2p6 286 bool builtinTypesAreCompatible(QualType, QualType); 287 bool vectorTypesAreCompatible(QualType, QualType); 288 289 /// Objective-C specific type checking. 290 bool interfaceTypesAreCompatible(QualType, QualType); 291 bool QualifiedInterfaceTypesAreCompatible(QualType, QualType); 292 bool objcTypesAreCompatible(QualType, QualType); 293 bool isObjcIdType(QualType T) const { 294 if (!IdStructType) // ObjC isn't enabled 295 return false; 296 return T->getAsStructureType() == IdStructType; 297 } 298 bool isObjcClassType(QualType T) const { 299 if (!ClassStructType) // ObjC isn't enabled 300 return false; 301 return T->getAsStructureType() == ClassStructType; 302 } 303 bool isObjcSelType(QualType T) const { 304 assert(SelStructType && "isObjcSelType used before 'SEL' type is built"); 305 return T->getAsStructureType() == SelStructType; 306 } 307 308private: 309 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 310 void operator=(const ASTContext&); // DO NOT IMPLEMENT 311 312 void InitBuiltinTypes(); 313 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 314 315 //===--------------------------------------------------------------------===// 316 // Serialization 317 //===--------------------------------------------------------------------===// 318 319public: 320 void Emit(llvm::Serializer& S) const; 321 static ASTContext* Create(llvm::Deserializer& D); 322}; 323 324} // end namespace clang 325 326#endif 327