ASTContext.h revision 73322924127c873c13101b705dd823f5539ffa5f
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 <vector>
23
24namespace clang {
25  class TargetInfo;
26
27/// ASTContext - This class holds long-lived AST nodes (such as types and
28/// decls) that can be referred to throughout the semantic analysis of a file.
29class ASTContext {
30  std::vector<Type*> Types;
31  llvm::FoldingSet<ComplexType> ComplexTypes;
32  llvm::FoldingSet<PointerType> PointerTypes;
33  llvm::FoldingSet<ReferenceType> ReferenceTypes;
34  llvm::FoldingSet<ArrayType> ArrayTypes;
35  llvm::FoldingSet<VectorType> VectorTypes;
36  llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos;
37  llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos;
38  llvm::DenseMap<const RecordDecl*, const RecordLayout*> RecordLayoutInfo;
39public:
40  TargetInfo &Target;
41  Builtin::Context BuiltinInfo;
42
43  // Builtin Types.
44  QualType VoidTy;
45  QualType BoolTy;
46  QualType CharTy;
47  QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy;
48  QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
49  QualType UnsignedLongLongTy;
50  QualType FloatTy, DoubleTy, LongDoubleTy;
51  QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
52
53  ASTContext(TargetInfo &t, IdentifierTable &idents) : Target(t) {
54    InitBuiltinTypes();
55    BuiltinInfo.InitializeBuiltins(idents, Target);
56  }
57  ~ASTContext();
58
59  void PrintStats() const;
60
61  //===--------------------------------------------------------------------===//
62  //                           Type Constructors
63  //===--------------------------------------------------------------------===//
64
65  /// getComplexType - Return the uniqued reference to the type for a complex
66  /// number with the specified element type.
67  QualType getComplexType(QualType T);
68
69  /// getPointerType - Return the uniqued reference to the type for a pointer to
70  /// the specified type.
71  QualType getPointerType(QualType T);
72
73  /// getReferenceType - Return the uniqued reference to the type for a
74  /// reference to the specified type.
75  QualType getReferenceType(QualType T);
76
77  /// getArrayType - Return the unique reference to the type for an array of the
78  /// specified element type.
79  QualType getArrayType(QualType EltTy, ArrayType::ArraySizeModifier ASM,
80                        unsigned EltTypeQuals, Expr *NumElts);
81
82  /// getVectorType - Return the unique reference to a vector type of
83  /// the specified element type and size. VectorType must be a built-in type.
84  QualType getVectorType(QualType VectorType, unsigned NumElts);
85
86  /// getOCUVectorType - Return the unique reference to an OCU vector type of
87  /// the specified element type and size. VectorType must be a built-in type.
88  QualType getOCUVectorType(QualType VectorType, unsigned NumElts);
89
90  /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
91  ///
92  QualType getFunctionTypeNoProto(QualType ResultTy);
93
94  /// getFunctionType - Return a normal function type with a typed argument
95  /// list.  isVariadic indicates whether the argument list includes '...'.
96  QualType getFunctionType(QualType ResultTy, QualType *ArgArray,
97                           unsigned NumArgs, bool isVariadic);
98
99  /// getTypedefType - Return the unique reference to the type for the
100  /// specified typename decl.
101  QualType getTypedefType(TypedefDecl *Decl);
102
103  /// getTagDeclType - Return the unique reference to the type for the
104  /// specified TagDecl (struct/union/class/enum) decl.
105  QualType getTagDeclType(TagDecl *Decl);
106
107  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
108  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
109  QualType getSizeType() const;
110
111  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
112  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
113  QualType getPointerDiffType() const;
114
115  //===--------------------------------------------------------------------===//
116  //                         Type Sizing and Analysis
117  //===--------------------------------------------------------------------===//
118
119  /// getTypeInfo - Get the size and alignment of the specified complete type in
120  /// bits.
121  std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L);
122
123  /// getTypeSize - Return the size of the specified type, in bits.  This method
124  /// does not work on incomplete types.
125  uint64_t getTypeSize(QualType T, SourceLocation L) {
126    return getTypeInfo(T, L).first;
127  }
128
129  /// getTypeAlign - Return the alignment of the specified type, in bits.  This
130  /// method does not work on incomplete types.
131  unsigned getTypeAlign(QualType T, SourceLocation L) {
132    return getTypeInfo(T, L).second;
133  }
134
135  /// getRecordLayout - Get or compute information about the layout of the
136  /// specified record (struct/union/class), which indicates its size and field
137  /// position information.
138  const RecordLayout &getRecordLayout(const RecordDecl *D, SourceLocation L);
139
140  //===--------------------------------------------------------------------===//
141  //                            Type Operators
142  //===--------------------------------------------------------------------===//
143
144  /// getIntegerBitwidth - Return the bitwidth of the specified integer type
145  /// according to the target.  'Loc' specifies the source location that
146  /// requires evaluation of this property.
147  unsigned getIntegerBitwidth(QualType T, SourceLocation Loc);
148
149  /// maxIntegerType - Returns the highest ranked integer type. Handles 3
150  /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned.
151  static QualType maxIntegerType(QualType lhs, QualType rhs);
152
153  /// maxFloatingType - Returns the highest ranked float type. Both input
154  /// types are required to be floats.
155  static QualType maxFloatingType(QualType lt, QualType rt);
156
157  /// maxComplexType - Returns the highest ranked complex type. Handles 3
158  /// different type combos: complex/complex, complex/float, float/complex.
159  QualType maxComplexType(QualType lt, QualType rt) const;
160
161private:
162  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
163  void operator=(const ASTContext&); // DO NOT IMPLEMENT
164
165  void InitBuiltinTypes();
166  void InitBuiltinType(QualType &R, BuiltinType::Kind K);
167};
168
169}  // end namespace clang
170
171#endif
172