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