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