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