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