ASTContext.h revision fee0452973f28691a61aab0fb074468ce3e34b9b
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 /// getObjcEncodingForMethodDecl - Return the encoded type for this method 189 /// declaration. 190 void getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl, std::string &S); 191 192 /// getObjcEncodingTypeSize returns size of type for objective-c encoding 193 /// purpose. 194 int getObjcEncodingTypeSize(QualType t); 195 196 // This setter/getter repreents the ObjC 'id' type. It is setup lazily, by 197 // Sema. 198 void setObjcIdType(TypedefDecl *Decl); 199 QualType getObjcIdType() const { return ObjcIdType; } 200 201 void setObjcSelType(TypedefDecl *Decl); 202 QualType getObjcSelType() const { return ObjcSelType; } 203 204 void setObjcProtoType(TypedefDecl *Decl); 205 QualType getObjcProtoType() const { return ObjcProtoType; } 206 207 void setObjcClassType(TypedefDecl *Decl); 208 QualType getObjcClassType() const { return ObjcClassType; } 209 210 void setBuiltinVaListType(QualType T); 211 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 212 213 //===--------------------------------------------------------------------===// 214 // Type Sizing and Analysis 215 //===--------------------------------------------------------------------===// 216 217 /// getTypeInfo - Get the size and alignment of the specified complete type in 218 /// bits. 219 std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L); 220 221 /// getTypeSize - Return the size of the specified type, in bits. This method 222 /// does not work on incomplete types. 223 uint64_t getTypeSize(QualType T, SourceLocation L) { 224 return getTypeInfo(T, L).first; 225 } 226 227 /// getTypeAlign - Return the alignment of the specified type, in bits. This 228 /// method does not work on incomplete types. 229 unsigned getTypeAlign(QualType T, SourceLocation L) { 230 return getTypeInfo(T, L).second; 231 } 232 233 /// getRecordLayout - Get or compute information about the layout of the 234 /// specified record (struct/union/class), which indicates its size and field 235 /// position information. 236 const RecordLayout &getRecordLayout(const RecordDecl *D, SourceLocation L); 237 238 //===--------------------------------------------------------------------===// 239 // Type Operators 240 //===--------------------------------------------------------------------===// 241 242 /// maxIntegerType - Returns the highest ranked integer type. Handles 3 243 /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned. 244 static QualType maxIntegerType(QualType lhs, QualType rhs); 245 246 /// compareFloatingType - Handles 3 different combos: 247 /// float/float, float/complex, complex/complex. 248 /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1. 249 static int compareFloatingType(QualType lt, QualType rt); 250 251 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 252 /// point or a complex type (based on typeDomain/typeSize). 253 /// 'typeDomain' is a real floating point or complex type. 254 /// 'typeSize' is a real floating point or complex type. 255 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 256 QualType typeDomain) const; 257 258 //===--------------------------------------------------------------------===// 259 // Type Compatibility Predicates 260 //===--------------------------------------------------------------------===// 261 262 /// Compatibility predicates used to check assignment expressions. 263 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 264 bool tagTypesAreCompatible(QualType, QualType); // C99 6.2.7p1 265 bool pointerTypesAreCompatible(QualType, QualType); // C99 6.7.5.1p2 266 bool referenceTypesAreCompatible(QualType, QualType); // C++ 5.17p6 267 bool functionTypesAreCompatible(QualType, QualType); // C99 6.7.5.3p15 268 bool arrayTypesAreCompatible(QualType, QualType); // C99 6.7.5.2p6 269 bool builtinTypesAreCompatible(QualType, QualType); 270 271 /// Objective-C specific type checking. 272 bool interfaceTypesAreCompatible(QualType, QualType); 273 bool objcTypesAreCompatible(QualType, QualType); 274 bool isObjcIdType(QualType T) const { 275 assert(IdStructType && "isObjcIdType used before 'id' type is built"); 276 return T->getAsStructureType() == IdStructType; 277 } 278 bool isObjcClassType(QualType T) const { 279 assert(ClassStructType && "isObjcClassType used before 'Class' type is built"); 280 return T->getAsStructureType() == ClassStructType; 281 } 282 bool isObjcSelType(QualType T) const { 283 assert(SelStructType && "isObjcSelType used before 'SEL' type is built"); 284 return T->getAsStructureType() == SelStructType; 285 } 286 287private: 288 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 289 void operator=(const ASTContext&); // DO NOT IMPLEMENT 290 291 void InitBuiltinTypes(); 292 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 293 294 //===--------------------------------------------------------------------===// 295 // Serialization 296 //===--------------------------------------------------------------------===// 297 298 void Emit(llvm::Serializer& S) const; 299 static ASTContext* Materialize(llvm::Deserializer& D); 300}; 301 302} // end namespace clang 303 304#endif 305