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