ASTContext.h revision b800dc2d5e27ec60f567567b623cdc61152b8fb8
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ASTContext.h - Context to hold long-lived AST nodes ----*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
55f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file was developed by Chris Lattner and is distributed under
65f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// the University of Illinois Open Source License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file defines the ASTContext interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_AST_ASTCONTEXT_H
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_AST_ASTCONTEXT_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/Builtins.h"
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/Expr.h"
19464175bba1318bef7905122e9fda20cff926df78Chris Lattner#include "clang/AST/RecordLayout.h"
20464175bba1318bef7905122e9fda20cff926df78Chris Lattner#include "clang/AST/Type.h"
21464175bba1318bef7905122e9fda20cff926df78Chris Lattner#include "llvm/ADT/DenseMap.h"
22a9aa5e69d8253e196be90c070738ab5bd9813730Steve Naroff#include "llvm/ADT/StringMap.h"
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <vector>
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class TargetInfo;
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ASTContext - This class holds long-lived AST nodes (such as types and
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// decls) that can be referred to throughout the semantic analysis of a file.
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass ASTContext {
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  std::vector<Type*> Types;
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSet<ComplexType> ComplexTypes;
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSet<PointerType> PointerTypes;
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSet<ReferenceType> ReferenceTypes;
35fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  llvm::FoldingSet<ConstantArrayType> ArrayTypes;
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSet<VectorType> VectorTypes;
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos;
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos;
39464175bba1318bef7905122e9fda20cff926df78Chris Lattner  llvm::DenseMap<const RecordDecl*, const RecordLayout*> RecordLayoutInfo;
4071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  RecordDecl *CFConstantStringTypeDecl;
41a9aa5e69d8253e196be90c070738ab5bd9813730Steve Naroff  llvm::StringMap<char> SelectorNames;
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
43b800dc2d5e27ec60f567567b623cdc61152b8fb8Chris Lattner  SourceManager &SourceMgr;
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  TargetInfo &Target;
4571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  IdentifierTable &Idents;
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Builtin::Context BuiltinInfo;
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Builtin Types.
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType VoidTy;
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType BoolTy;
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType CharTy;
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy;
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType UnsignedLongLongTy;
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType FloatTy, DoubleTy, LongDoubleTy;
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
58b800dc2d5e27ec60f567567b623cdc61152b8fb8Chris Lattner  ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents) :
59b800dc2d5e27ec60f567567b623cdc61152b8fb8Chris Lattner    CFConstantStringTypeDecl(0), SourceMgr(SM), Target(t), Idents(idents) {
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinTypes();
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BuiltinInfo.InitializeBuiltins(idents, Target);
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ~ASTContext();
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats() const;
66d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner
67464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //===--------------------------------------------------------------------===//
68464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //                           Type Constructors
69464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //===--------------------------------------------------------------------===//
70d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getComplexType - Return the uniqued reference to the type for a complex
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// number with the specified element type.
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getComplexType(QualType T);
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getPointerType - Return the uniqued reference to the type for a pointer to
765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the specified type.
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getPointerType(QualType T);
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getReferenceType - Return the uniqued reference to the type for a
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// reference to the specified type.
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getReferenceType(QualType T);
825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
83bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff  /// getVariableArrayType - Returns a non-unique reference to the type for a
84bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff  /// variable array of the specified element type.
85c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff  QualType getVariableArrayType(QualType EltTy, Expr *NumElts,
86c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                ArrayType::ArraySizeModifier ASM,
87c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                unsigned EltTypeQuals);
88fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
89bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff  /// getConstantArrayType - Return the unique reference to the type for a
90bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff  /// constant array of the specified element type.
91c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff  QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize,
92c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                ArrayType::ArraySizeModifier ASM,
93c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                unsigned EltTypeQuals);
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  /// getVectorType - Return the unique reference to a vector type of
9673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  /// the specified element type and size. VectorType must be a built-in type.
9773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType getVectorType(QualType VectorType, unsigned NumElts);
9873322924127c873c13101b705dd823f5539ffa5fSteve Naroff
9973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  /// getOCUVectorType - Return the unique reference to an OCU vector type of
10073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  /// the specified element type and size. VectorType must be a built-in type.
10173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType getOCUVectorType(QualType VectorType, unsigned NumElts);
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getFunctionTypeNoProto(QualType ResultTy);
1065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getFunctionType - Return a normal function type with a typed argument
1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// list.  isVariadic indicates whether the argument list includes '...'.
1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getFunctionType(QualType ResultTy, QualType *ArgArray,
1105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                           unsigned NumArgs, bool isVariadic);
1115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getTypedefType - Return the unique reference to the type for the
1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// specified typename decl.
1145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getTypedefType(TypedefDecl *Decl);
1153536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  QualType getObjcInterfaceType(ObjcInterfaceDecl *Decl);
1165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
117d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  /// getTypeOfType - GCC extension.
1188d1a3b8ca1e5fcc4567b5a6f51d82be2e460de1cSteve Naroff  QualType getTypeOfExpr(Expr *e);
119d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff  QualType getTypeOfType(QualType t);
120d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
1215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getTagDeclType - Return the unique reference to the type for the
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// specified TagDecl (struct/union/class/enum) decl.
1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getTagDeclType(TagDecl *Decl);
1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType getSizeType() const;
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1298b9023ba35a86838789e2c9034a6128728c547aaChris Lattner  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1308b9023ba35a86838789e2c9034a6128728c547aaChris Lattner  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1318b9023ba35a86838789e2c9034a6128728c547aaChris Lattner  QualType getPointerDiffType() const;
1328b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
13371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  // getCFConstantStringType - Return the type used for constant CFStrings.
13471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  QualType getCFConstantStringType();
13571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
136464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //===--------------------------------------------------------------------===//
137464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //                         Type Sizing and Analysis
138464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //===--------------------------------------------------------------------===//
139464175bba1318bef7905122e9fda20cff926df78Chris Lattner
140464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// getTypeInfo - Get the size and alignment of the specified complete type in
141464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// bits.
142464175bba1318bef7905122e9fda20cff926df78Chris Lattner  std::pair<uint64_t, unsigned> getTypeInfo(QualType T, SourceLocation L);
143464175bba1318bef7905122e9fda20cff926df78Chris Lattner
144464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// getTypeSize - Return the size of the specified type, in bits.  This method
145464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// does not work on incomplete types.
146464175bba1318bef7905122e9fda20cff926df78Chris Lattner  uint64_t getTypeSize(QualType T, SourceLocation L) {
147464175bba1318bef7905122e9fda20cff926df78Chris Lattner    return getTypeInfo(T, L).first;
148464175bba1318bef7905122e9fda20cff926df78Chris Lattner  }
149464175bba1318bef7905122e9fda20cff926df78Chris Lattner
150464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// getTypeAlign - Return the alignment of the specified type, in bits.  This
151464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// method does not work on incomplete types.
152464175bba1318bef7905122e9fda20cff926df78Chris Lattner  unsigned getTypeAlign(QualType T, SourceLocation L) {
153464175bba1318bef7905122e9fda20cff926df78Chris Lattner    return getTypeInfo(T, L).second;
154464175bba1318bef7905122e9fda20cff926df78Chris Lattner  }
155464175bba1318bef7905122e9fda20cff926df78Chris Lattner
156464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// getRecordLayout - Get or compute information about the layout of the
157464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// specified record (struct/union/class), which indicates its size and field
158464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// position information.
159464175bba1318bef7905122e9fda20cff926df78Chris Lattner  const RecordLayout &getRecordLayout(const RecordDecl *D, SourceLocation L);
160464175bba1318bef7905122e9fda20cff926df78Chris Lattner
161464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //===--------------------------------------------------------------------===//
162464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //                            Type Operators
163464175bba1318bef7905122e9fda20cff926df78Chris Lattner  //===--------------------------------------------------------------------===//
164464175bba1318bef7905122e9fda20cff926df78Chris Lattner
165464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// maxIntegerType - Returns the highest ranked integer type. Handles 3
166464175bba1318bef7905122e9fda20cff926df78Chris Lattner  /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned.
1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static QualType maxIntegerType(QualType lhs, QualType rhs);
1685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
169fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  /// compareFloatingType - Handles 3 different combos:
170fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  /// float/float, float/complex, complex/complex.
171fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
172fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  static int compareFloatingType(QualType lt, QualType rt);
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
174716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff  /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
175716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff  /// point or a complex type (based on typeDomain/typeSize).
176716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff  /// 'typeDomain' is a real floating point or complex type.
177716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff  /// 'typeSize' is a real floating point or complex type.
178f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
179f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff                                             QualType typeDomain) const;
180a9aa5e69d8253e196be90c070738ab5bd9813730Steve Naroff
181a9aa5e69d8253e196be90c070738ab5bd9813730Steve Naroff  //===--------------------------------------------------------------------===//
182a9aa5e69d8253e196be90c070738ab5bd9813730Steve Naroff  //                            Objective-C
183a9aa5e69d8253e196be90c070738ab5bd9813730Steve Naroff  //===--------------------------------------------------------------------===//
184a9aa5e69d8253e196be90c070738ab5bd9813730Steve Naroff
185a9aa5e69d8253e196be90c070738ab5bd9813730Steve Naroff  /// getSelectorName - Return a uniqued character string for the selector.
186a9aa5e69d8253e196be90c070738ab5bd9813730Steve Naroff  char &getSelectorName(const char *NameStart, const char *NameEnd) {
187a9aa5e69d8253e196be90c070738ab5bd9813730Steve Naroff    return SelectorNames.GetOrCreateValue(NameStart, NameEnd).getValue();
188a9aa5e69d8253e196be90c070738ab5bd9813730Steve Naroff  }
189a9aa5e69d8253e196be90c070738ab5bd9813730Steve Naroff
1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void operator=(const ASTContext&); // DO NOT IMPLEMENT
1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void InitBuiltinTypes();
1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void InitBuiltinType(QualType &R, BuiltinType::Kind K);
1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
201