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