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