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