ASTContext.h revision 3f128ad2691d299b96663da85a9e069c4081ea7c
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 class SelectorTable; 28 29/// ASTContext - This class holds long-lived AST nodes (such as types and 30/// decls) that can be referred to throughout the semantic analysis of a file. 31class ASTContext { 32 std::vector<Type*> Types; 33 llvm::FoldingSet<ComplexType> ComplexTypes; 34 llvm::FoldingSet<PointerType> PointerTypes; 35 llvm::FoldingSet<ReferenceType> ReferenceTypes; 36 llvm::FoldingSet<ConstantArrayType> ArrayTypes; 37 llvm::FoldingSet<VectorType> VectorTypes; 38 llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos; 39 llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos; 40 llvm::DenseMap<const RecordDecl*, const RecordLayout*> RecordLayoutInfo; 41 RecordDecl *CFConstantStringTypeDecl; 42 SelectorTable *Selectors; 43public: 44 SourceManager &SourceMgr; 45 TargetInfo &Target; 46 IdentifierTable &Idents; 47 Builtin::Context BuiltinInfo; 48 49 // Builtin Types. 50 QualType VoidTy; 51 QualType BoolTy; 52 QualType CharTy; 53 QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy; 54 QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy; 55 QualType UnsignedLongLongTy; 56 QualType FloatTy, DoubleTy, LongDoubleTy; 57 QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; 58 59 ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents) : 60 CFConstantStringTypeDecl(0), Selectors(0), 61 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 //===--------------------------------------------------------------------===// 164 // Type Operators 165 //===--------------------------------------------------------------------===// 166 167 /// maxIntegerType - Returns the highest ranked integer type. Handles 3 168 /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned. 169 static QualType maxIntegerType(QualType lhs, QualType rhs); 170 171 /// compareFloatingType - Handles 3 different combos: 172 /// float/float, float/complex, complex/complex. 173 /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1. 174 static int compareFloatingType(QualType lt, QualType rt); 175 176 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 177 /// point or a complex type (based on typeDomain/typeSize). 178 /// 'typeDomain' is a real floating point or complex type. 179 /// 'typeSize' is a real floating point or complex type. 180 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 181 QualType typeDomain) const; 182 183 //===--------------------------------------------------------------------===// 184 // Objective-C 185 //===--------------------------------------------------------------------===// 186 187 /// getSelectorInfo - Return a uniqued character string for the selector. 188 SelectorInfo &getSelectorInfo(const char *NameStart, const char *NameEnd); 189 190private: 191 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 192 void operator=(const ASTContext&); // DO NOT IMPLEMENT 193 194 void InitBuiltinTypes(); 195 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 196}; 197 198} // end namespace clang 199 200#endif 201