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