ASTContext.h revision 464175bba1318bef7905122e9fda20cff926df78
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  /// convertToVectorType - Return the unique reference to a vector type of
83  /// the specified element type and size. VectorType can be a pointer, array,
84  /// function, or built-in type (i.e. _Bool, integer, or float).
85  QualType convertToVectorType(QualType VectorType, unsigned NumElts);
86
87  /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
88  ///
89  QualType getFunctionTypeNoProto(QualType ResultTy);
90
91  /// getFunctionType - Return a normal function type with a typed argument
92  /// list.  isVariadic indicates whether the argument list includes '...'.
93  QualType getFunctionType(QualType ResultTy, QualType *ArgArray,
94                           unsigned NumArgs, bool isVariadic);
95
96  /// getTypedefType - Return the unique reference to the type for the
97  /// specified typename decl.
98  QualType getTypedefType(TypedefDecl *Decl);
99
100  /// getTagDeclType - Return the unique reference to the type for the
101  /// specified TagDecl (struct/union/class/enum) decl.
102  QualType getTagDeclType(TagDecl *Decl);
103
104  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
105  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
106  QualType getSizeType() const;
107
108  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
109  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
110  QualType getPointerDiffType() const;
111
112  //===--------------------------------------------------------------------===//
113  //                         Type Sizing and Analysis
114  //===--------------------------------------------------------------------===//
115
116  /// getTypeInfo - Get the size and alignment of the specified complete type in
117  /// bits.
118  std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L);
119
120  /// getTypeSize - Return the size of the specified type, in bits.  This method
121  /// does not work on incomplete types.
122  uint64_t getTypeSize(QualType T, SourceLocation L) {
123    return getTypeInfo(T, L).first;
124  }
125
126  /// getTypeAlign - Return the alignment of the specified type, in bits.  This
127  /// method does not work on incomplete types.
128  unsigned getTypeAlign(QualType T, SourceLocation L) {
129    return getTypeInfo(T, L).second;
130  }
131
132  /// getRecordLayout - Get or compute information about the layout of the
133  /// specified record (struct/union/class), which indicates its size and field
134  /// position information.
135  const RecordLayout &getRecordLayout(const RecordDecl *D, SourceLocation L);
136
137  //===--------------------------------------------------------------------===//
138  //                            Type Operators
139  //===--------------------------------------------------------------------===//
140
141  /// getIntegerBitwidth - Return the bitwidth of the specified integer type
142  /// according to the target.  'Loc' specifies the source location that
143  /// requires evaluation of this property.
144  unsigned getIntegerBitwidth(QualType T, SourceLocation Loc);
145
146  /// maxIntegerType - Returns the highest ranked integer type. Handles 3
147  /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned.
148  static QualType maxIntegerType(QualType lhs, QualType rhs);
149
150  /// maxFloatingType - Returns the highest ranked float type. Both input
151  /// types are required to be floats.
152  static QualType maxFloatingType(QualType lt, QualType rt);
153
154  /// maxComplexType - Returns the highest ranked complex type. Handles 3
155  /// different type combos: complex/complex, complex/float, float/complex.
156  QualType maxComplexType(QualType lt, QualType rt) const;
157
158private:
159  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
160  void operator=(const ASTContext&); // DO NOT IMPLEMENT
161
162  void InitBuiltinTypes();
163  void InitBuiltinType(QualType &R, BuiltinType::Kind K);
164};
165
166}  // end namespace clang
167
168#endif
169