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