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