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