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