ASTContext.h revision 6cd862c631eb4fc10755c146bbeaea37c330e815
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<ArrayType> 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 - Return the unique reference to the type for an array of the 81 /// specified element type. 82 QualType getArrayType(QualType EltTy, ArrayType::ArraySizeModifier ASM, 83 unsigned EltTypeQuals, Expr *NumElts); 84 85 /// getVectorType - Return the unique reference to a vector type of 86 /// the specified element type and size. VectorType must be a built-in type. 87 QualType getVectorType(QualType VectorType, unsigned NumElts); 88 89 /// getOCUVectorType - Return the unique reference to an OCU vector type of 90 /// the specified element type and size. VectorType must be a built-in type. 91 QualType getOCUVectorType(QualType VectorType, unsigned NumElts); 92 93 /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. 94 /// 95 QualType getFunctionTypeNoProto(QualType ResultTy); 96 97 /// getFunctionType - Return a normal function type with a typed argument 98 /// list. isVariadic indicates whether the argument list includes '...'. 99 QualType getFunctionType(QualType ResultTy, QualType *ArgArray, 100 unsigned NumArgs, bool isVariadic); 101 102 /// getTypedefType - Return the unique reference to the type for the 103 /// specified typename decl. 104 QualType getTypedefType(TypedefDecl *Decl); 105 106 /// getTypeOfType - GCC extension. 107 QualType getTypeOfExpr(Expr *e); 108 QualType getTypeOfType(QualType t); 109 110 /// getTagDeclType - Return the unique reference to the type for the 111 /// specified TagDecl (struct/union/class/enum) decl. 112 QualType getTagDeclType(TagDecl *Decl); 113 114 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 115 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 116 QualType getSizeType() const; 117 118 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 119 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 120 QualType getPointerDiffType() const; 121 122 // getCFConstantStringType - Return the type used for constant CFStrings. 123 QualType getCFConstantStringType(); 124 125 //===--------------------------------------------------------------------===// 126 // Type Sizing and Analysis 127 //===--------------------------------------------------------------------===// 128 129 /// getTypeInfo - Get the size and alignment of the specified complete type in 130 /// bits. 131 std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L); 132 133 /// getTypeSize - Return the size of the specified type, in bits. This method 134 /// does not work on incomplete types. 135 uint64_t getTypeSize(QualType T, SourceLocation L) { 136 return getTypeInfo(T, L).first; 137 } 138 139 /// getTypeAlign - Return the alignment of the specified type, in bits. This 140 /// method does not work on incomplete types. 141 unsigned getTypeAlign(QualType T, SourceLocation L) { 142 return getTypeInfo(T, L).second; 143 } 144 145 /// getRecordLayout - Get or compute information about the layout of the 146 /// specified record (struct/union/class), which indicates its size and field 147 /// position information. 148 const RecordLayout &getRecordLayout(const RecordDecl *D, SourceLocation L); 149 150 /// getEnumDeclIntegerType - returns the integer type compatible with the 151 /// given enum type. 152 QualType getEnumDeclIntegerType(EnumDecl *ED) const; 153 154 //===--------------------------------------------------------------------===// 155 // Type Operators 156 //===--------------------------------------------------------------------===// 157 158 /// getIntegerBitwidth - Return the bitwidth of the specified integer type 159 /// according to the target. 'Loc' specifies the source location that 160 /// requires evaluation of this property. 161 unsigned getIntegerBitwidth(QualType T, SourceLocation Loc); 162 163 /// maxIntegerType - Returns the highest ranked integer type. Handles 3 164 /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned. 165 static QualType maxIntegerType(QualType lhs, QualType rhs); 166 167 /// compareFloatingType - Handles 3 different combos: 168 /// float/float, float/complex, complex/complex. 169 /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1. 170 static int compareFloatingType(QualType lt, QualType rt); 171 172 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 173 /// point or a complex type (based on typeDomain/typeSize). 174 /// 'typeDomain' is a real floating point or complex type. 175 /// 'typeSize' is a real floating point or complex type. 176 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 177 QualType typeDomain) const; 178private: 179 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 180 void operator=(const ASTContext&); // DO NOT IMPLEMENT 181 182 void InitBuiltinTypes(); 183 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 184}; 185 186} // end namespace clang 187 188#endif 189