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