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