ASTContext.h revision 7e219e47de26346885d667131977bd9ca2d7662a
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 RecordDecl *CFConstantStringTypeDecl; 44 45 /// BuiltinVaListType - built-in va list type. 46 /// This is initially null and set by Sema::LazilyCreateBuiltin when 47 /// a builtin that takes a valist is encountered. 48 QualType BuiltinVaListType; 49 50 /// ObjcIdType - a psuedo built-in typedef type (set by Sema). 51 QualType ObjcIdType; 52 const RecordType *IdStructType; 53public: 54 55 SourceManager &SourceMgr; 56 TargetInfo &Target; 57 IdentifierTable &Idents; 58 SelectorTable &Selectors; 59 Builtin::Context BuiltinInfo; 60 61 // Builtin Types. 62 QualType VoidTy; 63 QualType BoolTy; 64 QualType CharTy; 65 QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy; 66 QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy; 67 QualType UnsignedLongLongTy; 68 QualType FloatTy, DoubleTy, LongDoubleTy; 69 QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; 70 71 ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents, 72 SelectorTable &sels) : 73 CFConstantStringTypeDecl(0), SourceMgr(SM), Target(t), 74 Idents(idents), Selectors(sels) { 75 InitBuiltinTypes(); 76 BuiltinInfo.InitializeBuiltins(idents, Target); 77 } 78 ~ASTContext(); 79 80 void PrintStats() const; 81 82 //===--------------------------------------------------------------------===// 83 // Type Constructors 84 //===--------------------------------------------------------------------===// 85 86 /// getComplexType - Return the uniqued reference to the type for a complex 87 /// number with the specified element type. 88 QualType getComplexType(QualType T); 89 90 /// getPointerType - Return the uniqued reference to the type for a pointer to 91 /// the specified type. 92 QualType getPointerType(QualType T); 93 94 /// getReferenceType - Return the uniqued reference to the type for a 95 /// reference to the specified type. 96 QualType getReferenceType(QualType T); 97 98 /// getVariableArrayType - Returns a non-unique reference to the type for a 99 /// variable array of the specified element type. 100 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 101 ArrayType::ArraySizeModifier ASM, 102 unsigned EltTypeQuals); 103 104 /// getConstantArrayType - Return the unique reference to the type for a 105 /// constant array of the specified element type. 106 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 107 ArrayType::ArraySizeModifier ASM, 108 unsigned EltTypeQuals); 109 110 /// getVectorType - Return the unique reference to a vector type of 111 /// the specified element type and size. VectorType must be a built-in type. 112 QualType getVectorType(QualType VectorType, unsigned NumElts); 113 114 /// getOCUVectorType - Return the unique reference to an OCU vector type of 115 /// the specified element type and size. VectorType must be a built-in type. 116 QualType getOCUVectorType(QualType VectorType, unsigned NumElts); 117 118 /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. 119 /// 120 QualType getFunctionTypeNoProto(QualType ResultTy); 121 122 /// getFunctionType - Return a normal function type with a typed argument 123 /// list. isVariadic indicates whether the argument list includes '...'. 124 QualType getFunctionType(QualType ResultTy, QualType *ArgArray, 125 unsigned NumArgs, bool isVariadic); 126 127 /// getTypedefType - Return the unique reference to the type for the 128 /// specified typename decl. 129 QualType getTypedefType(TypedefDecl *Decl); 130 QualType getObjcInterfaceType(ObjcInterfaceDecl *Decl); 131 132 /// getObjcQualifiedInterfaceType - Return a 133 /// ObjcQualifiedInterfaceType type for the given interface decl and 134 /// the conforming protocol list. 135 QualType getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl, 136 ObjcProtocolDecl **ProtocolList, unsigned NumProtocols); 137 138 /// getTypeOfType - GCC extension. 139 QualType getTypeOfExpr(Expr *e); 140 QualType getTypeOfType(QualType t); 141 142 /// getTagDeclType - Return the unique reference to the type for the 143 /// specified TagDecl (struct/union/class/enum) decl. 144 QualType getTagDeclType(TagDecl *Decl); 145 146 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 147 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 148 QualType getSizeType() const; 149 150 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 151 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 152 QualType getPointerDiffType() const; 153 154 // getCFConstantStringType - Return the type used for constant CFStrings. 155 QualType getCFConstantStringType(); 156 157 void setObjcIdType(TypedefDecl *Decl); 158 QualType getObjcIdType() const { return ObjcIdType; } 159 160 void setBuiltinVaListType(QualType T); 161 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 162 163 //===--------------------------------------------------------------------===// 164 // Type Sizing and Analysis 165 //===--------------------------------------------------------------------===// 166 167 /// getTypeInfo - Get the size and alignment of the specified complete type in 168 /// bits. 169 std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L); 170 171 /// getTypeSize - Return the size of the specified type, in bits. This method 172 /// does not work on incomplete types. 173 uint64_t getTypeSize(QualType T, SourceLocation L) { 174 return getTypeInfo(T, L).first; 175 } 176 177 /// getTypeAlign - Return the alignment of the specified type, in bits. This 178 /// method does not work on incomplete types. 179 unsigned getTypeAlign(QualType T, SourceLocation L) { 180 return getTypeInfo(T, L).second; 181 } 182 183 /// getRecordLayout - Get or compute information about the layout of the 184 /// specified record (struct/union/class), which indicates its size and field 185 /// position information. 186 const RecordLayout &getRecordLayout(const RecordDecl *D, SourceLocation L); 187 188 //===--------------------------------------------------------------------===// 189 // Type Operators 190 //===--------------------------------------------------------------------===// 191 192 /// maxIntegerType - Returns the highest ranked integer type. Handles 3 193 /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned. 194 static QualType maxIntegerType(QualType lhs, QualType rhs); 195 196 /// compareFloatingType - Handles 3 different combos: 197 /// float/float, float/complex, complex/complex. 198 /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1. 199 static int compareFloatingType(QualType lt, QualType rt); 200 201 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 202 /// point or a complex type (based on typeDomain/typeSize). 203 /// 'typeDomain' is a real floating point or complex type. 204 /// 'typeSize' is a real floating point or complex type. 205 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 206 QualType typeDomain) const; 207private: 208 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 209 void operator=(const ASTContext&); // DO NOT IMPLEMENT 210 211 void InitBuiltinTypes(); 212 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 213}; 214 215} // end namespace clang 216 217#endif 218